【发布时间】:2016-03-04 13:16:35
【问题描述】:
我不希望任何人为我编写代码。我只是在寻找朝着框架和/或使用方法的正确方向前进。我想要做的是,在 iOS Swift 中,允许用户选择一张照片并将其放在屏幕上。我已经让那部分工作了。我也有它的工作方式,您可以拖动图像来定位它,并捏合以使其更大或更小。然而,“棘手”的部分如下......当一个(或多个)边缘接近超级视图(屏幕)的框架时,我希望 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
}
}
【问题讨论】:
-
您是想在捏合过程中捕捉,还是只在拖动过程中捕捉?
-
另外,你如何控制图像视图的位置和大小?你在设置它的框架吗?你在设置它的变换吗?你在更新它的约束吗?
-
@robmayoff 我两个都喜欢
-
@robmayoff 我编辑了我的问题以包含我的代码到目前为止
标签: ios swift uiimageview frame