Animation cho hình ảnh được xoá
Animation mở nắp, đóng nắp của Trash
Animation làm cho Trash nghiêng một góc để hứng lấy hình ảnh bị xoá đưa vào Trash và đưa nó trở về vị ví hiện tại sau khi đã xoá xong hình ảnh.
Suck Effect
Đầu tiên, chúng ta sẽ viết code tạo animation xoá hình ảnh:
Sau đây là đoạn code thực hiện animation xoá cho _kImageView:
- (void)makeTheSuckAnimationForImage{
[UIView beginAnimations:@"suck" context:_kImageView];
[UIView setAnimationTransition:103 forView:_kImageView cache:NO];
[UIView setAnimationDuration:1.0f];
_kImageView.hidden = YES;// this code is important
//this is position where animation ends
[UIView setAnimationPosition:CGPointMake(280, _kBodyCanButton.frame.origin.y+10)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
Trong đoạn code trên chúng ta cần lưu ý đến:
+ Tên của animation : "Suck"
+ Trong câu lệnh [UIView setAnimationTransition:103 forView:_kImageView cache:NO]; thì số 103: là con số đại diện cho animation "suckEffect" (Anh em có thể xem thêm chi tiết ở link này:
http://iphonedevwiki.net/index.php/UIViewAnimationState#Using_suckEffect)
+_kImageView.hidden = YES : đây là câu lênh quan trọng, nếu không có nó thì trong quá trình thực hiện animation cho _kImageView, ảnh sẽ vẫn còn hiện diện mà không bay vào thùng rác theo ý muốn của mình.
Wobble Effect
1. animation mở nắp/đóng nắp thùng rác
Tại thời điểm thực hiện SuckEffect, chúng ta sẽ tạo animation mở nắp của thùng rác ra, code như sau:
- (void)wobbleAnimationForLidTrash:(UIButton *)imageView withAngle:(CGFloat) angle andAnchorPoint:(CGPoint) anchorPoint{
CGAffineTransform wobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(angle));
CGRect frame = imageView.frame;
[imageView.layer setAnchorPoint:anchorPoint];
[imageView setFrame:frame];
[UIView beginAnimations:@"wobble" context:imageView];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(closeLid:finished:context:)];
imageView.transform = wobble; // end here & auto-reverse
[UIView commitAnimations];
}//After finish animation, the lid will be closed
- (void) closeLid:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
if ([finished boolValue]) {
[self performSelector:@selector(closeTheLidTrashAnimation) withObject:nil afterDelay:0.5];
}
}
//Close the lid of trash Animation
-(void)closeTheLidTrashAnimation{
CGAffineTransform wobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(0));
CGPoint anchorPoint = CGPointMake(1.0, 1.0);
[_kLidCanButton.layer setAnchorPoint:anchorPoint];
[UIView beginAnimations:@"wobble" context:testButton];
[UIView setAnimationRepeatCount:1];
[UIView setAnimationDuration:0.15];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(makeMoreWobbleAnimation)];
_kLidCanButton.transform = wobble; // end here & auto-reverse
[UIView commitAnimations];
}
- (void)makeMoreWobbleAnimation{
_kBodyCanButton.transform = CGAffineTransformIdentity;
[_kLidCanButton.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
[_kBodyCanButton.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
_kLidCanButton.frame = lidOldFrame;
_kBodyCanButton.frame = bodyOldFrame;
//sau khi nap cua thung rac da duoc dong lai,
//chung ta tao them animation lam rung rung thung rac
[self wobbleAnimation:_kBodyCanButton];
[self wobbleAnimation:_kLidCanButton];
}
Trong đoạn code trên có một số điểm cần lưu ý:
+ Các Objects truyền vào là : đối tượng cần thực hiện animation, cần xoay bao nhiêu độ, anchor point của đối tượng đó. Trong ứng dụng của chúng ta thì chúng ta cần thực hiện animation cho _kLidButton, với một góc khoảng 140 độ và anchorPoint là (1.0,0.5)
+ Tên animation là "wobble"
Và tiếp theo chúng ta thực hiện animation nghiêng thùng rác để hứng hình ảnh được xoá đưa vào nhé:
Khi click vào button này, action sheet sẽ được show lên để cofirm việc xoá một hình ảnh này hay không. Thứ tự cho việc thực hiện toàn bộ animation xoá hình ảnh như sau:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch (buttonIndex) {
case 0:
{
[self wobbleAnimationForLidTrash:_kLidCanButton withAngle:140 andAnchorPoint:CGPointMake(1.0, 0.5)];
[self wobblingAnimationForBodyTrash:_kBodyCanButton withAngle:-10 andAnchorPoint:CGPointMake(0.0, 0.0)];
[self performSelector:@selector(makeTheSuckAnimationForImage) withObject:self afterDelay:0.3];
}
break;
default:
break;
}
}
Vào đây để download source