2016-11-18 5 views
1

私のUIViewControllerのメインビューでは、mapViewと、mapViewの上にあるビュー(別のビューAを考えてみましょう)があります。どちらもself.view.boundsと等しいフレームを持ちます。ビューAは、イメージを切り抜くのに使用されるのと同様のサイズ変更可能な矩形です。ここでの目標は、ユーザーがマップ上のエリアを指定できるようにすることです。だから、私は、ビューAを実現不可能な広場にすることはそれをあまりにも制限しすぎるので、ユーザーが地図の外を拡大して矩形の幅と高さの比率を変更できるようにしたい。内側からタッチをキャンセルすることはできますか?touchesBeganまたはtouchesMoved?

私はこのプロジェクトをGitHub https://github.com/justwudi/WDImagePickerから入手しました。これは、サイズ変更可能な矩形機能を使用しています。 Githubのリンクの2番目の画像には、8つの点と外に影の付いた四角形があります。ユーザーが陰影のある領域に触れると、ビューAの後ろにある地図にタッチを通過させたいと思っています。ユーザーが点の内側の領域または点の上をクリックした場合(つまり、長方形のサイズを変更したい場合)、ビュー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の通知のみ、この方法では動作しません。

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

アップデートソリューション: は、私の知る限り理解し、あなたがもしそうなら、あなたはUIControlクラスimplementationからcancelTracking(with:)機能をself.touchesCancelled(touches, with: event)への呼び出しを置き換えるために試すことができ、あなたのオーバーレイUIViewにタッチハンドラを実装しました、hitTest:

私は可能な解決策を確認しました。不必要なタッチ認識を避けるために。次の例はSwift Playgroundです。触ってドラッグしてコンソールで何が起こるかを見ることができます。

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(イベント付き)を呼び出すと、予期した結果が得られません。 – rgoncalv

+0

このコードはiOS 10.0のバージョンでは正常に動作していますが、iOS 11.0のバージョンでは動作しません....最新のコードを提供してください.....前もって感謝します –

関連する問題