iOS动画实现:弹簧效果
来源:互联网 发布:赛博朋克 知乎 编辑:程序博客网 时间:2024/06/11 01:50
源码地址:https://github.com/thermogl/TISpringLoadedViews,
这个比较复杂,我写了个简化版的:
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动画之弹簧效果
- Android 弹簧动画效果
- 弹簧动画效果
- Android 弹簧动画效果
- 果冻弹簧效果动画
- 弹簧动画效果
- ios弹簧动画
- 仿IOS效果-带弹簧动画的ListView
- 【Android】实现一个弹簧(Spring)效果的动画
- iOS UIScrollview 取消弹簧效果
- iOS:CAAnimation动画集合:移动/旋转/缩放/弹簧/翻页效果/落叶动画
- 类似弹簧跳动的动画效果
- iOS 8 Spring Animation[IOS 7.0以后动画效果超赞]---弹簧震动效果
- iOS中弹簧动画函数的解释
- ios UITableview头部图片弹簧效果
- SharePoint 2010/2013 禁止复制打印 webpart
- 矩阵累积和的最大路径
- Handler 引起的内存泄露
- XML简单的增改删操作
- InputStream 和OutputStream
- iOS动画实现:弹簧效果
- ActivityGroup相关--getLocalActivityManager() 以及intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)用法
- 关于伪静态网站如何做404错误
- 【转】Java 解析优酷地址获取视频真实地址
- 一个按钮读取U盘信息,在U盘中直接运行时,程序关闭,在其他盘中运行时却正常显示对话框
- cmake简洁教程
- Android SeekBar自定义使用图片和颜色显示
- RabbitMQ 三种Exchange
- java netty之NioSocketChannel