Swift洗牌动画效果的实现方法
作者:beatman_z 时间:2023-06-21 14:01:56
目标效果: 点击动画按钮之后每张牌各自旋转 散开到屏幕上半部分的任意位置之后回到初始位置 比较像LOL男刀的技能动画 : )
1: 创建卡牌对象
for _ in 0...49 {
let cardSet = UIImageView(image: UIImage(named: "cardBackLandscape"))
self.view.addSubview(cardSet)
cardSet.frame = self.landscapeCardBack.frame
self.cardSetList.append(cardSet)
}
NSNotificationCenter.defaultCenter().postNotificationName("setCreated", object: nil)
把每个卡牌作为UIImageView创建出来,为了之后对这些牌进行操作 我用数组把他们持有住 在同一位置创建好了之后 使用本地通知发送setCreated消息 告诉这个页面的观察者card set已经创建完毕 可以开始执行第二步动画
2: 首先需要把开始动画的按钮的用户交互关闭,如果开着的话连续点击每次都会创建50张牌,导致程序卡顿甚至挂掉
这里的delayTime是给线程加一个延迟时间 只是为了让动画不很生硬
每次循环给对应下标的card对象添加旋转动画,并且改变它的原点,我在用UIView动画实现这套动画之前想过给每张牌添加贝塞尔曲线,那样的话确实可控性更高,但是由于时间关系我还是只用了UIViewAnimation,给card添加的旋转动画是使用POP动画库实现的,这里使用的是Basic动画.这一步结束之后会把每张牌旋转并散开到不同的位置,在delayTime结束并触发本地通知发送shuffleFinished的时候,这个页面的观察者会执行下一部动画 也就是把每张牌还原到动画起点
func shuffleTheSet() {
self.shuffleButton.userInteractionEnabled = false
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.5 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
NSNotificationCenter.defaultCenter().postNotificationName("shuffleFinished", object: nil)
}
for count in 0...49 {
UIView.animateWithDuration(0.3, animations: {
let cardRotateAnimation = POPBasicAnimation(propertyNamed: kPOPLayerRotation)
cardRotateAnimation.fromValue = 0
cardRotateAnimation.toValue = CGFloat(M_PI * 2.0)
cardRotateAnimation.duration = 1
// cardRotateAnimation.duration = Double(count>5 ? count/2 : count/10)
cardRotateAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
self.cardSetList[count].layer.pop_addAnimation(cardRotateAnimation, forKey: "cardRotation")
self.cardSetList[count].frame.origin = CGPointMake(CGFloat(arc4random()) % (250 - 0 + 1) + 0, CGFloat(arc4random()) % (300 - 74 + 1) + 74)
self.view.layoutIfNeeded()
self.landscapeCardBack.removeFromSuperview()
})
}
}
3: 把每张牌的还原到初始位置,并把button的title设置为切牌状态.
for count in 0...49 {
UIView.animateWithDuration(0.3, animations: {
self.cardSetList[count].center = self.landscapeCardBack.center
})
self.view.layoutIfNeeded()
}
self.shuffleButton.userInteractionEnabled = true
self.shuffleButton.setTitle("Cut Card", forState: .Normal)
牌洗完之后的需求是切牌,由于时间原因下周继续更新后续动画效果…
以上所述是小编给大家介绍的Swift洗牌动画效果的实现方法,希望对大家有所帮助
来源:http://blog.csdn.net/beatman_z/article/details/53876670
标签:swift,洗牌,动画效果
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Spring的refresh方法你知道吗
2023-07-08 11:53:18
![](https://img.aspxhome.com/file/2023/5/67095_0s.png)
基于Jasypt对SpringBoot配置文件加密
2023-07-13 20:13:03
![](https://img.aspxhome.com/file/2023/9/125239_0s.png)
Android重力传感器实现滚动的弹球
2023-05-04 05:49:28
![](https://img.aspxhome.com/file/2023/7/102597_0s.png)
Android根据不同身份配置APP对应的不同模块方法
2023-09-25 05:22:36
![](https://img.aspxhome.com/file/2023/4/114354_0s.jpg)
深入分析C#中WinForm控件之Dock顺序调整的详解
2022-08-21 18:45:57
java.util.Collection源码分析与深度理解
2022-07-31 09:05:52
![](https://img.aspxhome.com/file/2023/5/80215_0s.png)
C++共享内存删除的陷阱
2022-04-03 12:30:37
Android studio 3.0上进行多渠道打包遇到的问题小结(超简洁版)
2022-09-16 07:58:40
Java毕业设计实战之图片展览馆管理系统的实现
2021-06-16 08:23:18
![](https://img.aspxhome.com/file/2023/1/62521_0s.jpg)
Android launcher中模拟按home键的实现
2023-03-25 02:33:48
SpringCloud Zuul网关功能实现解析
2022-09-12 14:30:31
![](https://img.aspxhome.com/file/2023/3/67453_0s.png)
Spring深入探索AOP切面编程
2023-05-27 09:37:16
![](https://img.aspxhome.com/file/2023/6/78056_0s.png)
mybatis-plus 返回部分字段的解决方式
2023-03-25 14:45:04
![](https://img.aspxhome.com/file/2023/1/61281_0s.jpg)
聊聊Java并发中的Synchronized
2022-07-26 03:19:24
![](https://img.aspxhome.com/file/2023/7/89567_0s.jpg)
resty mail的简单发送邮件方法
2021-09-15 06:00:14
Java使用BigDecimal进行运算封装的实际案例
2023-06-20 02:22:26
Java面向对象基础知识之封装,继承,多态和抽象
2022-11-18 07:35:59
Android实现自动轮询的RecycleView
2023-10-17 15:45:17
C#权限管理和设计浅谈
2023-10-09 07:14:45
基于Spring AOP proxyTargetClass的行为表现总结
2022-05-23 18:33:43