iOS动画实现:弹簧效果
2013-06-28 16:10
337 查看
源码地址:https://github.com/thermogl/TISpringLoadedViews,
这个比较复杂,我写了个简化版的:
https://www.dropbox.com/s/sv3yhm8dovh0adq/SpringDemo.zip http://download.csdn.net/detail/u010180166/5665977
这个方法由CADisplayLink负责调用,每秒钟默认60次。要看懂这段代码,首先你要了解弹簧的原理,比如拉起弹簧收到的力怎么计算,往回弹收到的阻力怎么计算。
下面是手势操作的代码,也就是拖动时触发的方法:
这个比较复杂,我写了个简化版的:
https://www.dropbox.com/s/sv3yhm8dovh0adq/SpringDemo.zip http://download.csdn.net/detail/u010180166/5665977
- (void)simulateSpringWithDisplayLink:(CADisplayLink *)displayLink { if (springEnabled && !self.panning){ CGPoint displacement = CGPointMake(self.center.x - restCenter.x, self.center.y - restCenter.y); //控件收到的力 CGPoint kx = CGPointMake(springConstant * displacement.x, springConstant * displacement.y); //控件受到的阻力 CGPoint bv = CGPointMake(dampingCoefficient * velocity.x, dampingCoefficient * velocity.y); //加速度 CGPoint acceleration = CGPointMake((kx.x + bv.x) / mass, (kx.y + bv.y) / mass); velocity.x -= (acceleration.x * displayLink.duration); velocity.y -= (acceleration.y * displayLink.duration); //设置控件新位置 CGPoint newCenter = self.center; newCenter.x += (velocity.x * displayLink.duration); newCenter.y += (velocity.y * displayLink.duration); [self setCenter:newCenter]; } }
这个方法由CADisplayLink负责调用,每秒钟默认60次。要看懂这段代码,首先你要了解弹簧的原理,比如拉起弹簧收到的力怎么计算,往回弹收到的阻力怎么计算。
下面是手势操作的代码,也就是拖动时触发的方法:
- (void)viewWasPanned:(UIPanGestureRecognizer *)sender { CGPoint translation = CGPointApplyAffineTransform([sender translationInView:self.superview], CGAffineTransformMakeScale(panDragCoefficient, panDragCoefficient)); CGPoint translatedCenter = CGPointMake(self.center.x + translation.x, self.center.y + translation.y); if (translation.x > 0 && (translatedCenter.x - restCenter.x) > panDistanceLimits.right){ translation.x -= (translatedCenter.x - restCenter.x) - panDistanceLimits.right; } else if (translation.x < 0 && (restCenter.x - translatedCenter.x) > panDistanceLimits.left){ translation.x += (restCenter.x - translatedCenter.x) - panDistanceLimits.left; } if (translation.y > 0 && (translatedCenter.y - restCenter.y) > panDistanceLimits.bottom){ translation.y -= (translatedCenter.y - restCenter.y) - panDistanceLimits.bottom; } else if (translation.y < 0 && (restCenter.y - translatedCenter.y) > panDistanceLimits.top){ translation.y += (restCenter.y - translatedCenter.y) - panDistanceLimits.top; } [self setCenter:CGPointMake(self.center.x + translation.x, self.center.y + translation.y)]; [sender setTranslation:CGPointZero inView:self.superview]; }
相关文章推荐
- iOS动画实现:弹簧效果
- iOS动画实现:弹簧效果
- iOS的动画效果类型及实现方法
- iOS动画效果和实现
- IOS 利用麦克风声音来实现吹裙子的动画效果
- iOS的动画效果类型及实现方法
- iOS pushViewController 实现push 和pop 默认动画效果
- iOS的动画效果类型及实现方法
- ios 组合动画实现图片浏览,效果为图片放大并淡出
- IOS开发-UIView之动画效果的实现方法(合集)
- 仿IOS效果 带弹簧动画的ListView
- iOS开发之iOS中的动画实现,通过CAShapeLayer、CAShapeLayer,CAReplicatorLayer实现炫酷的动画、雷达效果,波纹效果,咻一咻效果。iOS核心动画实现。
- 学习iOS动画效果并实现在viewController的转跳上
- iOS 转盘动画效果实现
- iOS使用UICountingLabel实现数字变化的动画效果
- iOS 利用动画和贝塞尔实现咻咻效果
- ios基于MJRefresh实现上拉刷新和下拉加载动画效果
- iOS动画效果和实现
- iOS开发:粘性动画以及果冻效果的实现
- iOS动画效果和实现