我不希望任何人爲我編寫代碼。我只是在朝着框架和/或使用方法的正確方向尋求推動。我想要做的是,在iOS Swift中,允許用戶選擇一張照片並將其放置在屏幕上。我已經有那部分工作。我也有它的工作,你可以通過拖動圖像來定位它,並捏合使其變大或變小。然而,「棘手」的部分是以下...當邊緣(或邊緣)接近超級視圖(屏幕)的框架時,我希望ImageView能夠「捕捉」到邊緣的位置。基本上,我希望圖像的邊緣被「磁性」吸引到屏幕的邊緣。我不太確定哪種方法組合最適合完成這一點。任何建議感激。如何獲取UIImageView的邊緣以捕捉到屏幕邊緣(超級視圖)?
編輯: 這裏是我迄今爲止...
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我更喜歡 – Lastmboy