2016-03-04 86 views
0

我不希望任何人爲我編寫代碼。我只是在朝着框架和/或使用方法的正確方向尋求推動。我想要做的是,在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 
    } 
} 
+0

你是否想在夾捏過程中捕捉對齊,或者只是在拖動過程中? –

+0

另外,你如何控制圖像視圖的位置和大小?你正在設置其框架?你正在設置它的變換嗎?你在更新它的限制嗎? –

+0

@robmayoff我更喜歡 – Lastmboy

回答

0

的聲音,我想要的財產以後像則hitTest。

Hittest

所以,我想你不會想和拖放工作。儘快觸發一個marge(5-10或15dp/px)觸發器,你鎖定圖像=>停止拖動並以編程方式定位視圖...我沒有測試這個,但是trhat是我的第一個方法。

hittest subview

雖然你不想與子視圖但有子視圖的邊緣測試,我認爲這將是可能的。

snapping to edge

上面的鏈接看起來像一個平滑邊緣剪裁。扭轉邏輯,你應該能夠剪輯到邊界...

+1

很多偉大的東西在這裏。謝謝。我會做一些實驗並回報。 – Lastmboy

+0

期待。未來可能需要:) – SamuelD

+1

測試結果如何?還是你找到另一種解決方案? – SamuelD

0

你可以嘗試使用一種方法來檢查UIImageView的邊緣是否在超級視圖的框架的一定距離內。

func checkEdge(view: UIImageView) { 

    let left = view.frame.minX 
    let right = view.frame.maxX 
    let bottom = view.frame.maxY 
    let top = view.frame.minY 

    if left <= 10 { 
     // Move view to left side 
    } else if right <= 10 { 
     // Move view to right side 
    } else if bottom <= 10 { 
     // Move view to bottom 
    } else if top <= 10 { 
     // Move view to top 
    } else { 
     // Do nothing 
    } 
} 

讓我知道這是否工作和/或你在找什麼!

+0

我得到這個錯誤:二進制運算符'<='不能應用於類型'NSLayoutYAxisAnchor'和'Int'的操作數 – Lastmboy

+0

好吧讓我來看一下,我會回覆你 – mccoyLBI

+0

@Lastmboy看到我的代碼修訂版我的答案 – mccoyLBI