我不希望有人为我编写代码。我只是在寻找朝着正确方向插入框架和/或使用方法的插入力。我想要做的是,在 iOS Swift 中,允许用户选择一张照片并将其放置在屏幕上。我已经让那部分工作了。我还可以使用它,您可以拖动图像来定位它,并捏合以使其更大或更小。然而,“棘手”的部分如下......当一个边缘(或多个边缘)接近 super View (屏幕)的框架时,我希望 ImageView 在边缘“卡入”到位。基本上,我希望图像的边缘被“磁性”吸引到屏幕的边缘。我不太确定哪种方法组合最适合实现此目的。任何建议表示赞赏。
编辑: 这是我到目前为止所拥有的...
class AdjustableImageView: UIImageView {
var parent:ViewController!
// last location for view
var lastSavedLocation = CGPointZero
override init(frame: CGRect) {
super.init(frame: frame)
// add pan gesture to view
let panGesture = UIPanGestureRecognizer(target: self, action: "handlePanGesture:")
let longPressGesture = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
let pinchGesture = UIPinchGestureRecognizer(target: self, action: "pinchRecognized:")
self.addGestureRecognizer(panGesture)
self.addGestureRecognizer(longPressGesture)
self.addGestureRecognizer(pinchGesture)
self.userInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func pinchRecognized(pinch: UIPinchGestureRecognizer) {
// change view scale based on pinch
self.transform = CGAffineTransformScale(self.transform, pinch.scale, pinch.scale)
pinch.scale = 1.0
}
func handlePanGesture(gesture: UIPanGestureRecognizer) {
// find translation in main view
let newTranslation = gesture.translationInView(self.superview)
// set current object to new position
self.center = CGPointMake(self.lastSavedLocation.x + newTranslation.x , self.lastSavedLocation.y + newTranslation.y)
}
// detect touch for the current view and change last save postion
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// move touched view to front.
self.superview?.bringSubviewToFront(self)
self.layer.borderColor = UIColor.greenColor().CGColor
self.layer.borderWidth = 2
// save view location
self.lastSavedLocation = self.center
parent.imageSelected(self)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.layer.borderWidth = 0
}
}
最佳答案
在我看来,你想要一些像热门测试这样的东西。
所以我认为您想要使用拖放操作。一旦 HitTest 触发marge(5-10或15dp/px),您就锁定图像=>停止拖动并以编程方式定位 View ...我没有对此进行测试,但这将是我的第一个方法。
虽然您不想使用 subview 进行测试,但我认为使用 subview 的边缘是可能的。
上面的链接看起来像是平滑的边缘剪辑。颠倒逻辑,您应该能够剪辑到边界...
关于ios - 如何让 UIImageView 的边缘捕捉到屏幕( super View )的边缘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35797064/