swift果冻效果
来源:互联网 发布:a5源码交易平台 编辑:程序博客网 时间:2024/04/24 09:05
在网上看到一个果冻效果的demo,在他的基础上加上自己的需求,用swift实现了一下
自定义一个类MyCuteView
import UIKitlet deviceWidth = UIScreen.mainScreen().bounds.size.widthlet deviceHeigth = UIScreen.mainScreen().bounds.size.heightlet minHeight:CGFloat = 100.0class MyCuteView: UIView { var mHeigth:CGFloat = 0.0 dynamic var curveX:CGFloat = 0.0 // dynamic 修饰才走观察者的代理方法 dynamic var curveY:CGFloat = 0.0 var cureView:UIView? let shapeLayer:CAShapeLayer = CAShapeLayer() var displayLink:CADisplayLink? var isAnimating:Bool? //初始化 override init(frame: CGRect) { super.init(frame: frame) //注册观察者 self.addObserver(self, forKeyPath:"curveX", options:.New, context: nil) self.addObserver(self, forKeyPath:"curveY", options:.New, context: nil) configShapeLayer() configCurveView() configAction() } //观察者方法 override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) { if(keyPath == "curveX" || keyPath == "curveY"){ updateShapeLayerPath() } } //手势 func configAction(){ mHeigth = 100 // 手势移动时相对高度 isAnimating = false // 是否处于动效状态 // 手势 let pan : UIPanGestureRecognizer! = UIPanGestureRecognizer.init(target: self, action: Selector("handlePanAction:")) self.userInteractionEnabled = true self .addGestureRecognizer(pan) //CADisplayLink默认每秒运行60次calculatePath是算出在运行期间_curveView的坐标,从而确定_shapeLayer的形状 displayLink = CADisplayLink(target: self, selector: Selector("update")) displayLink?.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes) displayLink?.paused = true } //shapLayer(也是) func configShapeLayer() { shapeLayer.fillColor = UIColor.yellowColor().CGColor layer.addSublayer(shapeLayer) } //小红点 func configCurveView() { curveX = deviceWidth / 2.0 curveY = minHeight cureView = UIView.init(frame: CGRect.init(x: curveX, y: curveY , width: 3, height: 3)) cureView!.backgroundColor = UIColor.redColor() self.addSubview(cureView!) } func handlePanAction(pan:UIPanGestureRecognizer) ->Void{ if isAnimating == false { if (pan.state == .Changed){ // 手势移动时,_shapeLayer跟着手势向下扩大区域 let point:CGPoint = pan.translationInView(self) mHeigth = point.y*0.7 + minHeight curveX = deviceWidth / 2.0 + point.x curveY = mHeigth > minHeight ? mHeigth : minHeight cureView?.frame = CGRect.init(x: curveX, y: curveY, width: (cureView?.frame.size.width)!, height: (cureView?.frame.size.height)!) }else if (pan.state == .Cancelled || pan.state == .Failed || pan.state == .Ended){ isAnimating = true // 手势结束时,_shapeLayer返回原状并产生弹簧动效 displayLink!.paused = false //开启displaylink,会执行方法 UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity:0, options:.OverrideInheritedDuration, animations: { () -> Void in self.cureView?.frame = CGRect.init(x: deviceWidth/2.0, y: minHeight, width: 3, height: 3) }, completion: { (Bool) -> Void in self.displayLink!.paused = true self.isAnimating = false }) } } } func updateShapeLayerPath() -> Void{ // 更新_shapeLayer形状 let tPath = UIBezierPath.init() tPath.moveToPoint(CGPoint.init(x: 0, y: 0)) tPath.addLineToPoint(CGPoint.init(x: deviceWidth, y: 0)) tPath.addLineToPoint(CGPoint.init(x: deviceWidth, y: minHeight)) tPath.addQuadCurveToPoint(CGPoint.init(x: 0, y: minHeight), controlPoint: CGPoint.init(x: curveX, y: curveY)) tPath.closePath() shapeLayer.path = tPath.CGPath } func update() -> Void{ // 由于手势结束时,r5执行了一个UIView的弹簧动画,把这个过程的坐标记录下来,并相应的画出_shapeLayer形状 let layer = cureView?.layer.presentationLayer() curveX = layer!.position.x; curveY = layer!.position.y; } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } //删除 deinit { self.removeObserver(self, forKeyPath: "curveX") self.removeObserver(self, forKeyPath: "curveY") }}
在VC中调用
import UIKitclass ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let cuteView = MyCuteView.init(frame: CGRectMake(0, 0, 320, 568)) cuteView.backgroundColor = UIColor.whiteColor() self.view .addSubview(cuteView) }
0 0
- swift果冻效果
- 果冻效果原理解析
- 果冻弹簧效果动画
- animation 实现果冻突出效果
- jq实现果冻抖动效果
- 粘性动画以及果冻效果
- Android果冻效果滑动控件
- Android果冻效果滑动控件
- cocos2d-lua 按钮果冻效果
- 果冻回弹效果 拉刷新效果 UIBezierPath
- cocos2dx 实现果冻,刀光等效果
- 使用CADisplayLink实现果冻效果动画
- Android果冻效果(阻尼动画)
- 粘性动画以及果冻效果的实现
- ios 用 UIBezierPath 实现果冻效果
- iOS开发 - 用 UIBezierPath 实现果冻效果
- 利用html5/css3 动画效果,实现图片果冻抖动效果
- 谈谈iOS中粘性动画以及果冻效果的实现
- CTS测试时手机的设置
- 如何使用springmvc框架在普通的servlet注入service
- iOS开发之百度地图的简单集成——标注&POI检索
- Android 数据库(SQLite)的导入导出命令
- nfs挂载出现no such device是内核没有配置NFS
- swift果冻效果
- linux中fork()函数详解
- jquery中,size()和length的区别
- iOS使用高德地图趟过的坑([MAMapKit] apiKey为空,请检查key是否正确设置)
- 汉诺塔问题--算法
- 解决“启动程序失败,路径或者权限错误”的执行问题
- ubuntu下安装程序的三种方法
- 文本语言模型的参数估计-最大似然估计、MAP及贝叶斯估计及跟NLP推荐的好博客
- ,谈谈final, finally, finalize的区别