IOS 触摸获取坐标点、缩放图片实例
2015-10-02 19:10
501 查看
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { println("began") } override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { println("move") println((touches as NSSet).anyObject()?.locationInView(self.view))//移动时输出当前坐标 } override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { println("end") }
其中第一个touches:是触摸点坐标集合
还可以开启多点触摸
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.view.multipleTouchEnabled = true //多点触摸开启 }
同时输出这两个点得坐标位置
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { println("move") // println((touches as NSSet).anyObject()?.locationInView(self.view)) for touch in (touches as NSSet){ println(touch.locationInView(self.view)) } }
缩放图片实例
在全局先定义一个浮点类型的变量,用来当做距离长度
//定义最后一次的操作 private var lastDistance:CGFloat = 0.0
在touchesMoved函数中
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { // println("move") // println((touches as NSSet).anyObject()?.locationInView(self.view)) // for touch in (touches as NSSet){ // println(touch.locationInView(self.view)) // } //缩放即判断两点移动前后的距离是变大变小 if touches.count == 2{ //触摸点是否等于2 var a = (touches as NSSet).allObjects[0].locationInView(self.view) //第一个点 var b = (touches as NSSet).allObjects[1].locationInView(self.view) //第二个点 //计算x与y的差 var xx = a.x - b.x var yy = a.y - b.y //c为两点间距离 var c = sqrt(xx*xx+yy*yy) if lastDistance == 0.0{ lastDistance = c //lastDistance一开始初始化为0.0,所以把距离赋值给 lastDistance后进行再进行距离差距的比较 }else{ if lastDistance - c > 5{ println("缩小") lastDistance = c //图片的缩放 iv.transform = CGAffineTransformScale(iv.transform, 0.9, 0.9) }else if lastDistance - c < -5{ println("放大") lastDistance = c iv.transform = CGAffineTransformScale(iv.transform, 1.1, 1.1) } } } }
相关文章推荐
- [iOS开发项目-6] 应用程序管理
- IOS总结 静变量static、全局变量extern、局部变量、实例变量
- IOS自定义动画
- iOS <CoreLocation>定位框架的理解与运用
- iOS8 沙盒路径变化特性
- IOS动画效果
- ios申请真机调试( xcode 5)详细解析
- iOS 远程推送
- ios动画
- mac Xcode/iOS license
- IOS视图切换
- iOS 多线程的使用(GCD)
- iOS 多线程的使用(Operation)
- iOS 多线程实例(自定义NSOperation并传值(block,notification))
- iOS 多线程实例(NSThread售票窗口的模拟)
- iOS开发TTS技术即文本转语音
- iOS多线程使用实例(异步下载图片的时候不会阻塞主线程的执行)
- iOS导航栏的颜色和电池颜色的修改
- IOS—静态方法(类方法)和实例方法
- IOS实时预览自定义控件效果