最近研究了一下,Swift语言中关于Animation动画的实现学习,分两次进行相关内容的讲解
用表格列出各种动画情况
Demo首页显示展示了一种动画显示方式,代码如下:
//绘画装饰
func drawDecorate(){
//画出小圆
let smallCenterPoint = CGPointMake(50, 50);
let smallRadiusFloat:CGFloat = sqrt(800);
let smallMaskPath = UIBezierPath.init(ovalInRect: CGRectInset(CGRectMake(smallCenterPoint.x, smallCenterPoint.y, 1, 1), -smallRadiusFloat, -smallRadiusFloat));
//画出大圆
let largeCenterPoint = CGPointMake(50, 50);
let largeRadiusFloat = sqrt(pow(view.bounds.width-largeCenterPoint.x, 2)+pow(view.bounds.height-largeCenterPoint.y, 2));
let largerMaskPath = UIBezierPath.init(ovalInRect: CGRectInset(CGRectMake(largeCenterPoint.x, largeCenterPoint.y, 1, 1), -largeRadiusFloat, -largeRadiusFloat));
//表层图画
let maskLayer = CAShapeLayer();
maskLayer.path = largerMaskPath.CGPath;
view.layer.mask = maskLayer;
//基础动画
let baseAnimation = CABasicAnimation.init(keyPath: "path");
baseAnimation.duration = 0.5;
baseAnimation.fromValue = smallMaskPath.CGPath;
baseAnimation.toValue = largerMaskPath.CGPath;
baseAnimation.delegate = self;
maskLayer.addAnimation(baseAnimation, forKey: "path");
}
然后今天说明的是:1:点击进行滑动动画;2:点击进行两次动画效果;3:基本动画;4:键值动画;5:转场动画
部分代码:
1:点击进行滑动动画;
(可以添加渐变效果)
//# MARK:- 用UIView.animateWithDuration实现更逼真的运动效果
func view1Clicked(tapGesture:UITapGestureRecognizer){
//view1动画,向左移动100
let theView = tapGesture.view!
//usingSpringWithDamping 阻尼,范围0-1,阻尼越接近于0,弹性效果越明显
UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 1, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
theView.frame = CGRect(x: theView.frame.origin.x+100, y: theView.frame.origin.y, width: theView.frame.size.width, height: theView.frame.height)
}, completion: nil)
}
2:点击进行两次动画效果;
//CATransaction 配置隐式动画
func view1Clicked(tapGesture:UITapGestureRecognizer){
CATransaction.begin()
//CATranscation 属性
//设置动画执行时间
CATransaction.setAnimationDuration(1)
//关闭隐式动画,这句话必须放在修改属性之前
// CATransaction.setDisableActions(true)
//设置动画完成后的回调
CATransaction.setCompletionBlock { () -> Void in
NSLog("Animation complete")
}
CATransaction.setAnimationDuration(1)
self.myLayer.backgroundColor = UIColor.greenColor().CGColor
self.myLayer.opacity = 0.5
var moveToPoint = CGPoint(x: myLayer.position.x + 150, y: myLayer.position.y + 50)
if(moveToPoint.x > view.frame.size.width) { moveToPoint.x -= view.frame.size.width}
if(moveToPoint.y > view.frame.size.height) { moveToPoint.y -= view.frame.size.height}
self.myLayer.position = moveToPoint
CATransaction.commit()
performSelector(#selector(TwoDonghuaViewController.pause), withObject: nil, afterDelay: 0.2)
}
3:基本动画;
//移动
func move(theView:UIView){
/*
可选的KeyPath
transform.scale = 比例轉換
transform.scale.x
transform.scale.y
transform.rotation = 旋轉
transform.rotation.x
transform.rotation.y
transform.rotation.z
transform.translation
transform.translation.x
transform.translation.y
transform.translation.z
opacity = 透明度
margin
zPosition
backgroundColor 背景颜色
cornerRadius 圆角
borderWidth
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
*/
let baseAnimation = CABasicAnimation(keyPath: "position")
//baseAnimation.fromValue 初始位置,如果不设就是当前位置
let endPoint = CGPoint(x: theView.layer.position.x+100, y: theView.layer.position.y)
baseAnimation.toValue = NSValue(CGPoint:endPoint)//绝对位置
//baseAnimation.byValue = NSValue(CGPoint:CGPoint(x: 100, y: 0))//相对位置
//动画其他属性
baseAnimation.duration = 0.2
baseAnimation.repeatCount = 1
baseAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)//加速运动
//baseAnimation.timingFunction = CAMediaTimingFunction(controlPoints: 0.5, 0, 0.9, 0.7)//自定义加速的曲线参数
//这两个属性若不设置,动画执行后回复位
baseAnimation.removedOnCompletion = false
baseAnimation.fillMode = kCAFillModeForwards
baseAnimation.delegate = self
//可以在动画中缓存一些
baseAnimation.setValue(NSValue(CGPoint: endPoint), forKey: "endPoint")
baseAnimation.setValue(theView, forKey: "sender")
//开始动画
theView.layer.addAnimation(baseAnimation, forKey: "theViewMoveRight100")
}
4:键值动画
func imageViewClicked(tapGesture:UITapGestureRecognizer){
//键值动画
//keyPath和basicAnimation的类型相同,@see BasicAnimationViewController.swift
let keyframeAnimation = CAKeyframeAnimation(keyPath: "position")
//弧线位置移动
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 50, 50)
CGPathAddCurveToPoint(path, nil, 50, 50, 700, 300, 30, 500)
keyframeAnimation.path = path
keyframeAnimation.calculationMode = kCAAnimationLinear
//设置其他属性
keyframeAnimation.duration = 1.0;
// keyframeAnimation.beginTime = CACurrentMediaTime() + 2;//设置延迟2秒执行
tapGesture.view?.layer.addAnimation(keyframeAnimation, forKey: "keyframeAnimation1")
tapGesture.view?.layer.presentationLayer()
tapGesture.view?.layer.modelLayer()
}
5:转场动画
func swipeTransition(subtype:String)->CATransition{
let transfer = CATransition()
/*
kCATransitionFade:淡入淡出,默认效果
kCATransitionMoveIn:新视图移动到就是图上方
kCATransitionPush:新视图推开旧视图
kCATransitionReveal:移走旧视图然后显示新视图
//苹果未公开的私有转场效果
cube:立方体
suckEffect:吸走的效果
oglFlip:前后翻转效果
rippleEffect:波纹效果
pageCurl:翻页起来
pageUnCurl:翻页下来
cameraIrisHollowOpen:镜头开
cameraIrisHollowClose:镜头关
*/
let types = [kCATransitionFade,kCATransitionMoveIn,kCATransitionPush,kCATransitionReveal,"cube","suckEffect","oglFlip","rippleEffect","pageCurl","pageUnCurl","cameraIrisHollowOpen","cameraIrisHollowClose"]
let type = types[Int(arc4random()%11)]
transfer.type = type
NSLog("动画类型。。。。%@", type)
transfer.subtype = subtype
transfer.duration = 1
return transfer
}
源代码:http://download.csdn.net/detail/hbblzjy/9647803,敬请关注,(感觉不错,想要代码的,请留言或关注我)。。。