看了看他的代碼,發(fā)現(xiàn)核心動(dòng)畫(就是把按鈕包裝成一個(gè)禮物盒)其實(shí)很簡(jiǎn)單,就是把一個(gè)動(dòng)畫的一幀一幀都截取下來(lái)放到一個(gè)數(shù)組里面,然后利用了UIImageView自帶的可以播放一個(gè)image的數(shù)組的方法。
簡(jiǎn)化過(guò)的代碼大概是這樣子:
- (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;
});
}
效果如圖:
方法雖然簡(jiǎn)單,但是效果很贊,不知道有沒(méi)有更好的實(shí)現(xiàn)方式,但這起碼是一種實(shí)現(xiàn)方式,值得記錄一下!