实现物理仿真的效果 鼠标点击处,小球弹到那里
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,weak)IBOutlet UIImageView *iconImageView;
@property(nonatomic,strong)UIDynamicAnimator *animator;
@end
@implementation ViewController
-(UIDynamicAnimator *)animator
{
if (!_animator) {
//创建物理仿真器(referecnce 物理仿真范围)
self.animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
}
return _animator;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:touch.view];
UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:self.iconImageView snapToPoint:point];
//减震
snap.damping = 0.5;
[self.animator removeAllBehaviors];//移除之前所有行为
[self.animator addBehavior:snap];//添加新的行为
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ios-UIDynamicAnimator(物理仿真动画)
最新推荐文章于 2021-04-18 11:40:30 发布