#import "ViewController.h" @interface ViewController () { UIImageView *_imageView; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ball"]]; _imageView.center = CGPointMake(375/2, 667/2); [self.view addSubview:_imageView]; } //立即點擊觸發彈性動畫 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self.view]; //建立彈性動畫 //阻尼:返回0-1 ,越接近0,彈性效果越明顯 //velocity:彈性復原速度 [UIView animateWithDuration:5.0 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{ _imageView.center = location;//最終小球位置 NSLog(@"%.2f----%.2f",location.x,location.y); }completion:nil]; } //點擊後鬆開手瞬間觸發彈性動畫 //-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // UITouch *touch = [touches anyObject]; // CGPoint location = [touch locationInView:self.view]; // [UIView animateWithDuration:5 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{ // _imageView.center = location; // } completion:nil]; //} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end