随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万

 

1. SB放上俩 imageview,拖线成类属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import UIKit
 
class ViewController: UIViewController {
 
    @IBOutlet weak var box2: UIImageView!
    @IBOutlet weak var box1: UIImageView!
     
    //1.s创建仿真器
       private lazy var animator: UIDynamicAnimator = UIDynamicAnimator(referenceView: self.view)
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置背景颜色图
        view.backgroundColor = UIColor(patternImage: UIImage.init(named: "background") ?? UIImage())
    }
     
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
         
        let point = touches.first?.location(in: view)
        snap(point: point ?? CGPoint.zero)
         
        gravity()
        collision()
        push()
    }
     
    ///重力
    func gravity() {
         
        //2.创建仿真行为
        let behavior = UIGravityBehavior(items: [box1])
         
        //重力方向,重力的力度
//        behavior.gravityDirection = CGVector(dx: 10, dy: -10)
         
//        //角度
//        behavior.angle =  -CGFloat.pi
//
//        //量级:速度
//        behavior.magnitude = 5
         
        //设置角度和量级
        behavior.setAngle( CGFloat.pi/2, magnitude: 5)
         
        //3. 仿真器添加仿真行为
        animator.addBehavior(behavior)
    }
     
     
    /// 碰撞
    func collision() {
         
        //2.创建仿真行为
        let behavior = UICollisionBehavior(items: [box1, box2])
         
        //添加碰撞边界: view的bounds
        behavior.translatesReferenceBoundsIntoBoundary = true
 
//        items: 只碰撞元素
//        boundaries:只碰撞边界
//        everything:任何东西
        //碰撞模式
        behavior.collisionMode = UICollisionBehavior.Mode.everything
         
        //设置边界的内边距
//        behavior.setTranslatesReferenceBoundsIntoBoundary(with: UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20))
         
        let str :  NSString = "123" // NSString 遵循NSCopying
        let str1 : String = "123"   //String 没有遵循NSCopying
        //添加一个边界
//        behavior.addBoundary(withIdentifier: str, from: CGPoint(x: 0, y: 500), to: CGPoint(x: view.frame.size.width, y: 300))
         
         
        //设置代理
        behavior.collisionDelegate = self
        //3. 仿真器添加仿真行为
        animator.addBehavior(behavior)
    }
     
 
    /// 创建捕捉行为
    func snap(point:CGPoint){
        //FIXME:如果想要多次执行p捕捉行为,必须先移除添加捕捉行为
        animator.removeAllBehaviors()
         
        let behavior = UISnapBehavior(item: box2, snapTo: point)
         
        //动画流畅度
        behavior.damping = 1
         
        animator.addBehavior(behavior)
    }
 
    func push(){
         
        // continuous:一直推
        // instantaneous:推一次
        //1.创建推行为
        let behavior = UIPushBehavior(items: [box1], mode: UIPushBehavior.Mode.instantaneous)
         
        //设置方向
        behavior.pushDirection = CGVector(dx: 0, dy: -8)
         
        //2. 仿真器添加仿真行为
        animator.addBehavior(behavior)
    }
     
}
 
 
extension ViewController:UICollisionBehaviorDelegate{
    func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item1: UIDynamicItem, with item2: UIDynamicItem, at p: CGPoint){
        print("开始碰撞 元素 - 元素", p)
    }
     
    func collisionBehavior(_ behavior: UICollisionBehavior, endedContactFor item1: UIDynamicItem, with item2: UIDynamicItem){
        print("结束碰撞 元素 - 元素")
    }
     
    func collisionBehavior(_ behavior: UICollisionBehavior, beganContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?, at p: CGPoint){
        print("开始碰撞 元素 - 边界", p)
    }
     
    func collisionBehavior(_ behavior: UICollisionBehavior, endedContactFor item: UIDynamicItem, withBoundaryIdentifier identifier: NSCopying?){
        print("结束碰撞 元素 - 边界")
    }
}

  

posted on   懂事长qingzZ  阅读(268)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示