UIImageView 內建動畫+N張圖片實現很炫的動畫,

來源:互聯網
上載者:User

UIImageView 內建動畫+N張圖片實現很炫的動畫,

gitHub上又看到個很炫的動畫:https://github.com/MartinRGB/GiftCard-iOS 看了看他的代碼,發現核心動畫(就是把按鈕封裝成一個禮物盒)其實很簡單,就是把一個動畫的一幀一幀都截取下來放到一個數組裡面,然後利用了UIImageView內建的可以播放一個image的數組的方法。簡化過的代碼大概是這樣子: - (void)viewDidLoad {    [super viewDidLoad];     NSMutableArray *array = [NSMutableArray array];    for (int i = 0; i < 40; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat: @"gifbutton000%02d",i]];
        [array addObject:image];
    }
    self.giftImageArray = array;
    self.giftImageView.animationDuration = 1.0;
    self.giftImageView.animationImages = self.giftImageArray;
    self.giftImageView.animationRepeatCount = 1;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGiftImageViewTapped:)];
    [self.giftImageView addGestureRecognizer:tapGesture];
    self.giftImageView.userInteractionEnabled = YES;
    self.giftImageView.image = [self.giftImageArray firstObject];
   } - (void)handleGiftImageViewTapped:(UITapGestureRecognizer *)sender {
    [self.giftImageView startAnimating];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIImage *image =  [self.giftImageArray lastObject];
        self.giftImageView.image = image;
    });}效果  方法雖然簡單,但是效果很贊,不知道有沒有更好的實現方式,但這起碼是一種實現方式,值得記錄一下!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.