ios – 如何识别触摸的图像

ios – 如何识别触摸的图像,第1张

概述我正在开发一个应用程序,用户可以在画布上拖放项目,当他释放图像时,它将在画布上绘制. 这是我的DragImage类,它处理触摸: class DragImages: UIImageView { var originalPos : CGPoint! var dropTarget: UIView? override init (frame : CGRect){ 我正在开发一个应用程序,用户可以在画布上拖放项目,当他释放图像时,它将在画布上绘制.

这是我的DragImage类,它处理触摸:

class DragImages: UIImageVIEw {    var originalPos : CGPoint!    var dropTarget: UIVIEw?    overrIDe init (frame : CGRect){        super.init(frame: frame)    }    required init?(coder aDecoder : NSCoder){        super.init(coder : aDecoder)    }    overrIDe func touchesBegan(_ touches : Set<UItouch>,with event: UIEvent?){        originalPos = self.center    }    overrIDe func touchesMoved(_ touches: Set<UItouch>,with event: UIEvent?) {        if let touch = touches.first{            let position = touch.location(in: self.supervIEw)            self.center = CGPoint(x : position.x,y : position.y)        }    }    overrIDe func touchesEnded(_ touches: Set<UItouch>,with event: UIEvent?) {        if let touch = touches.first,let target = dropTarget{            let position = touch.location(in: self.supervIEw)            if target.frame.contains(position){               NotificationCenter.default.post(Notification(name: Notification.name(rawValue: "onTargetDropped"),object: nil))            }else {                self.center = originalPos            }        }        print(self.center.x,self.center.y)        self.center = originalPos    }    func getEndPosX() -> CGfloat{        return self.center.x    }    func getEndPosY() -> CGfloat {        return self.center.y    }}

在我的VIEwController类中,我添加了这段代码来处理触摸等:

ornament1.dropTarget = xmasTree    ornament2.dropTarget = xmasTree    ornament3.dropTarget = xmasTree    ornament4.dropTarget = xmasTreeNotificationCenter.default.addobserver(self,selector: #selector(VIEwController.itemDroppedOnTree(_:)),name: NSNotification.name(rawValue: "onTargetDropped"),object: nil)}func itemDroppedOnTree(_ notif : AnyObject){}

当图像在画布上拖动时,我设法得到X和Y位置,但是我无法找到一种方法来识别4个图像中的哪一个被丢弃,以便我绘制特定的图像!

解决方法 您可以将发件人添加到您的通知(以及位置):

NotificationCenter.default.post(Notification(name: Notification.name(rawValue: "onTargetDropped"),object: self,userInfo: ["position":position]))

稍后在itemDroppedOnTree中获取它:

func itemDroppedOnTree(_ notif : NSNotification){    let position = notif.userInfo["position"]    let sender = notif.object as! DragImage    if sender === dragImage1 {         //...     } else if sender === dragImage2 {         //...    } }

我建议反对它,并恳求使用委托来通知VIEwController. (基于意见:通常,仅对多人广播使用通知.)

委托函数应将sender作为第一个参数.根据func tableVIEw:tableVIEw:UItableVIEw,cellForRowAt indexPath:IndexPath).

通过这种方式,您可以知道哪个图像正在发送其新位置,并可以将其与您的属性进行比较,如上例所示:

if dragImage === dragImage1 {...

您的代码加上工作委托粘贴到Playground:

import UIKitimport PlaygroundSupportprotocol DragImageDelegate: class {    func dragimage(_ dragImage:DragImage,dIDDropAt position:CGPoint)}class DragImage: UIImageVIEw {    weak var delegate: DragImageDelegate?    var originalPos : CGPoint!    var dropTarget: UIVIEw?    overrIDe init (frame : CGRect) {       super.init(frame: frame)        isUserInteractionEnabled = true    }    required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }    overrIDe func touchesBegan(_ touches : Set<UItouch>,with event: UIEvent?) {        if let touch = touches.first,let target = dropTarget {            let position = touch.location(in: self.supervIEw)            if target.frame.contains(position){                print(self.center.x,self.center.y)                guard let delegate = self.delegate else {                    print("delegate not set")                    return                }                print(self.center.x,self.center.y)                delegate.dragimage(self,dIDDropAt: position)                return            }        }        self.center = originalPos    }}class MyVC: UIVIEwController,DragImageDelegate {    let dragImage1 = DragImage(frame: CGRect(x: 0.0,y: 0.0,wIDth: 30.0,height: 30.0))    let dragImage2 = DragImage(frame: CGRect(x: 0.0,y: 100.0,height: 30.0))    overrIDe func vIEwDIDLoad() {        let target = UIVIEw(frame: CGRect(x: 200.0,y: 400.0,height: 30.0))        target.backgroundcolor = .black        vIEw.addSubvIEw(target)        dragImage1.backgroundcolor = .white        dragImage2.backgroundcolor = .white        dragImage1.dropTarget = target        dragImage2.dropTarget = target        vIEw.addSubvIEw(dragImage1)        vIEw.addSubvIEw(dragImage2)        dragImage1.delegate = self        dragImage2.delegate  = self    }    private func move(_ vIEw:UIVIEw,to position:CGPoint) {        vIEw.frame = CGRect(x: position.x,y: position.y,wIDth: vIEw.frame.size.wIDth,height: vIEw.frame.size.height)    }    // MARK: - DragImageDelegate    func dragimage(_ dragImage: DragImage,dIDDropAt position: CGPoint) {        if dragImage === dragImage1 {            move(dragImage1,to: position)        } else if dragImage === dragImage2 {            move(dragImage2,to: position)        }    }}var container = UIVIEw(frame: CGRect(x: 0.0,wIDth: 300.0,height: 600.0))let myVc = MyVC()myVc.vIEw.frame = CGRect(x: 0.0,height: 600.0)myVc.vIEw.backgroundcolor = .greencontainer.addSubvIEw(myVc.vIEw)PlaygroundPage.current.liveVIEw = container

结果:

总结

以上是内存溢出为你收集整理的ios – 如何识别触摸的图像全部内容,希望文章能够帮你解决ios – 如何识别触摸的图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1067699.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存