2016-11-18 75 views
1

在我的UIViewController主視圖中,我有一個mapView和另一個視圖(比方說視圖A),它位於mapView上方。它們都具有等於self.view.bounds的框架。視圖A是一個可調整大小的矩形,與用於裁剪圖像的矩形相似。我的目標是讓用戶在地圖上指定一個區域。所以,我希望用戶能夠放大地圖以及改變矩形的寬度和高度比例,因爲只讓視圖A成爲不可實現的方形會限制它太多。是否可以從touchesBegan或touchesMoved內部取消觸摸?

我從GitHub https://github.com/justwudi/WDImagePicker中得到了這個項目,我從中使用了可調整大小的矩形功能。在Github鏈接的第二張圖片中,有一個8個點的矩形和一個外面的陰影區域。如果用戶觸摸陰影區域,我希望能夠將觸摸傳遞到視圖A後面的地圖。只有當用戶點擊點或點上的區域(以便他想調整矩形的大小)時,我希望視圖A能夠識別觸摸。所以,我修改上的視圖觸摸的代碼,並有這樣的:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first { 

     if cropBorderView.frame.contains(touch.location(in: self)){ 
      print("touch contains - touchesbegan") 
      //self.isUserInteractionEnabled = true 
     } 
     else{ 
      print("Touch does not contain - touchesbegan") 
      self.touchesCancelled(touches, with: event) 
      //return 
     } 
     let touchPoint = touch.location(in: cropBorderView) 

     anchor = self.calculateAnchorBorder(touchPoint) 
     fillMultiplyer() 
     resizingEnabled = true 
     startPoint = touch.location(in: self.superview) 
    } 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    print("inside touches moved") 
    if let touch = touches.first { 
     if cropBorderView.frame.contains(touch.location(in: self)){ 
      print("touch contains - touchesmoved") 
      //self.isUserInteractionEnabled = true 
     } 
     else{ 
      print("Touch does not contain - touchesmoved ") 
      self.touchesCancelled(touches, with: event) 
      //return 
     } 

     if resizingEnabled! { 
      self.resizeWithTouchPoint(touch.location(in: self.superview)) 
     } 
    } 
} 

它確實承認,當我點擊內外觸摸,因爲我想要的,但是當我點擊外面停不觸摸。這意味着撥打self.touchesCancelled(touches, with: event)不起作用。調用返回會導致崩潰,並且不起作用。有沒有解決這個問題的方法?

謝謝你的時間和考慮。

回答

0

touchesCancelled(_:with:)只是通知UITouch,它不會這樣工作。 據我瞭解,你實現觸摸處理器在您的覆蓋UIView,如果是的話,你可以嘗試從UIControlimplementationcancelTracking(with:)函數調用替換到self.touchesCancelled(touches, with: event)

else { 
    print("Touch does not contain - touchesmoved ") 
    self.cancelTracking(with event) 
} 

更新解決方案,根據hitTest:

我檢查了可能的解決方案,似乎你可以使用hitTest:以避免不必要的觸摸識別。下面的例子是斯威夫特遊樂場,您可以點擊並拖動觸摸,並在控制檯中看到發生了什麼:

import UIKit 
import PlaygroundSupport 

class JSGView : UIView { 

    var centerView = UIView() 

    override func didMoveToSuperview() { 
     frame = CGRect(x: 0, y: 0, width: 320, height: 480) 
     backgroundColor = UIColor.clear 

     centerView.frame = CGRect(x: 110, y: 190, width: 100, height: 100) 
     centerView.backgroundColor = UIColor.blue 
     addSubview(centerView) 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     dump(event) 
    } 

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     if let touch = touches.first { 
      if (hitTest(touch.location(in: self), with: event) != nil) { 
       print("Touch passed hit test and seems valid") 
       super.touchesCancelled(touches, with: event) 
       return 
      } 
     } 

     print("Touch isn't passed hit test and will be ignored") 
     super.touchesMoved(touches, with: event) 
    } 

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 
     if centerView.bounds.contains(centerView.convert(point, from: self)) { 
      return centerView 
     } 
     return nil 
    } 
} 

class JSGViewController : UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     view.frame = CGRect(x: 0, y: 0, width: 320, height: 480) 
     let customView = JSGView() 
     view.addSubview(customView) 
    } 
} 

let controller = JSGViewController() 
PlaygroundPage.current.liveView = controller.view 
+0

此功能不可用在我的課,從只有UIView的繼承。當我嘗試使它從UIControl繼承以及我得到錯誤「從'UIView'和'UIControl'的多重繼承」「 – rgoncalv

+0

修復了上述問題。剛剛實現的UIControl繼承自UIView。但是,調用self.cancelTracking(帶有事件)未提供預期結果。沒有任何事情發生,因爲與self.touchesCledlled(觸及,與:事件) – rgoncalv

+0

此代碼工作正常在iOS 10.0 ....版本,但不在iOS 11.0版本工作....請提供最新的代碼.....提前致謝 –

相關問題