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;
});}效果 方法雖然簡單,但是效果很贊,不知道有沒有更好的實現方式,但這起碼是一種實現方式,值得記錄一下!