简单的仿真效果

效果图
简单的仿真效果_第1张图片
仿真效果.gif

#import "LQCHomeController.h" @interface LQCHomeController () @property (nonatomic ,strong) UIDynamicAnimator *animator;//仿真器 @property (nonatomic ,strong) UIView *redView; @end

@implementation LQCHomeController

- (UIDynamicAnimator *)animator
{
if (_animator == nil) {
_animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
} return _animator; }

- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; //添加子视图(添加想要的子视图类型) [self addChildViews]; }
#pragma mark 添加子视图
- (void)addChildViews { self.redView = [[UIView alloc]initWithFrame:CGRectMake(100, 300, 100, 100)];//子视图位置和大小(自己设置想要的位置) self.redView.backgroundColor = [UIColor redColor]; [self.view addSubview:self.redView]; }
#pragma mark 移动视图要实现的效果方法
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //1.想要移动时的仿真效果 //获取点 CGPoint movePoint = [[touches anyObject]locationInView:self.view]; [self addEmulatedBehaviorWithPoint:movePoint]; //2.不想在移动的时候有仿真效果 // // 获得UITouch对象(封装了用户对屏幕的触摸) // UITouch *touch = [touches anyObject]; // // 获得触摸点相对于当前视图父视图(窗口)的坐标 // CGPoint centerPoint = [touch locationInView:self.view]; // // 将当前视图的中心点设置为手指 // self.redView.center = centerPoint; }
#pragma mark 触摸结束时要实现的效果方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{ //获取点 CGPoint endPoint = CGPointMake(200, 300);//捕捉到哪个位置(自己设置想要的位置) [self addEmulatedBehaviorWithPoint:endPoint]; }
#pragma mark 仿真行为方法
- (void)addEmulatedBehaviorWithPoint:(CGPoint)point{ //1.将之前的捕捉行为移除 [self.animator removeAllBehaviors]; //3.创建捕捉行为 UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:self.redView snapToPoint:point]; //3.1.设置捕捉行为的阻力 snap.damping = 0.5; //4.将仿真行为添加到仿真器中 [self.animator addBehavior:snap]; }
@end

你可能感兴趣的:(简单的仿真效果)