動畫封裝--彈簧

#import "ViewController.h"

@interface ViewController ()
{
    UIImageView *image;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 50, 50)];
    image.image=[UIImage imageNamed:@"1"];
    [self.view addSubview:image];
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch=[touches anyObject];
    CGPoint location=[touch locationInView:self.view];
    //建立彈性動畫
    //usingSpringWithDamping 範圍0-1,越接近0,彈性效果越明顯
    //initialSpringVelocity  彈性復原速度
    [UIView animateWithDuration:5.0 delay:1.0 usingSpringWithDamping:0.2 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
        image.center=location;//最終小球的位置
    } completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
相關文章
相關標籤/搜索