gpt4 book ai didi

ios - 如何识别触摸了哪个图像

转载 作者:搜寻专家 更新时间:2023-10-30 23:09:17 25 4
gpt4 key购买 nike

我正在开发一个应用程序,用户可以在 Canvas 上拖放项目,当他释放图像时,它就会绘制在 Canvas 上。

这是我处理触摸的 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 = xmasTree

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.itemDroppedOnTree(_:)), name: NSNotification.Name(rawValue: "onTargetDropped"), object: nil)

}


func itemDroppedOnTree(_ notif : AnyObject){



}

当在 Canvas 上拖动图像时,我设法获得了 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 {
//...
}
}

不过我建议不要这样做,并请求使用 delegate 来通知 ViewController。 (基于意见:一般来说,仅将通知用于对多广播。)

委托(delegate)函数应该将发送者作为第一个参数。根据 func tableView: tableView:UITableView, cellForRowAt indexPath:IndexPath)

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

 if dragImage === dragImage1 {...

您的代码加上要粘贴到 Playground 的工作委托(delegate):

import UIKit
import PlaygroundSupport

protocol 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?){
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){
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, width: 30.0, height: 30.0))

override func viewDidLoad() {
let target = UIView(frame: CGRect(x: 200.0, y: 400.0, width: 30.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, y: 0.0, width: 300.0, height: 600.0))
let myVc = MyVC()
myVc.view.frame = CGRect(x: 0.0, y: 0.0, width: 300.0, height: 600.0)
myVc.view.backgroundColor = .green
container.addSubview(myVc.view)

PlaygroundPage.current.liveView = container

结果:

关于ios - 如何识别触摸了哪个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41045960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com