2015-01-26 4 views
1

두 개의보기가 서로 쌓여 있습니다. 보기 B가 더 큰보기 A 위에 쌓인다고 가정 해 봅시다.보기 B는 원형이며 더 큰보기 A가 접촉을 받아 들여야하는 대신 원의 바깥 부분을 받아 들여서는 안됩니다.원형 UIView가 원형 바깥에있는 접촉을 인식하지 못하게하는 방법은 무엇입니까?

나는 그것이 원형 경로와-있는지 확인하고이 같은 인식기 슈퍼에 취소 접촉 전화 뷰 B의 사용자 지정 PanGesture 인식기의 touchesBegan 메서드를 재정 시도했다 :

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) { 
    let touch = touches.anyObject() as UITouch 
    let location = touch.locationInView(self.view) 
    let isInPath = self.path.containsPoint(location) 

    if(isInPath){ 
     super.touchesBegan(touches, withEvent: event) 
     self.rotation = rotationForLocation(location) 
    }else{ 
     // touch started outside of path so cancel touch 
     super.cancelsTouchesInView = true 
     super.touchesCancelled(touches, withEvent: event) 
    } 
} 

그것을 보기 B가 원형 경로 외부의 접촉을 허용하지 않지만보기 A는 해당 접촉을 허용하지 않습니다. 어떤 도움을 주셔서 감사합니다, 감사합니다.

+1

hitTest 메서드를 재정의하십시오. – rmaddy

답변

1

당신은 원 이내 경우에만 제스처를 시작하는 gestureRecognizerShouldBegin 방법 대리자를 지정하는 것 중 하나는 두 개의 제스처 인식기를 추가 할 수 있습니다. 그런 다음 원형이 아닌 경우에만 비 원형 제스처를 성공시킬 수 있습니다.

// add square view 

let subview = UIView(frame: frame) 
subview.backgroundColor = UIColor.lightGrayColor() 
subview.userInteractionEnabled = true 
view.addSubview(subview) 

// add circular shapelayer (so I can see the circle) 

let circularShapeLayer = CAShapeLayer() 
circularShapeLayer.frame = subview.bounds 
circularShapeLayer.path = UIBezierPath(ovalInRect: subview.bounds).CGPath 
circularShapeLayer.fillColor = UIColor.darkGrayColor().CGColor 
subview.layer.addSublayer(circularShapeLayer) 

// add gesture that will use delegate method `gestureRecognizerShouldBegin` to determine if you started in the circle 

let circlePanGesture = UIPanGestureRecognizer(target: self, action: "handleCircularPan:") 
circlePanGesture.delegate = self 
subview.addGestureRecognizer(circlePanGesture) 

// add a gesture recognizer that will only be recognized if the prior gesture recognizer fails 

let squarePanGesture = UIPanGestureRecognizer(target: self, action: "handleSquarePan:") 
squarePanGesture.requireGestureRecognizerToFail(circlePanGesture) 
subview.addGestureRecognizer(squarePanGesture) 

위임 방법과 같습니다

func gestureRecognizerShouldBegin(gesture: UIGestureRecognizer) -> Bool { 
    let center = CGPoint(x: gesture.view!.bounds.size.width/2.0, y: gesture.view!.bounds.size.height/2.0) 
    let location = gesture.locationInView(gesture.view) 
    let distance = hypot(center.x - location.x, center.y - location.y) 
    return distance < (gesture.view!.bounds.size.width/2.0) 
} 

주, 나는 (동일한 서브 뷰에 이러한 동작을 모두 추가 해요 때문에 나는 userInteractionEnabled이 둘 중복 전망이있는 경우에만 몸짓을 얻는 경우에 최고 것).

+0

그런 분명한 대답에 감사드립니다. 명확하고 요점. –

0

터치를 취소하지 말고 A 객체로 전달하십시오.

+0

앞으로 A를 보시겠습니까? 그렇다면이 제스처 인식기가 사용자 정의 UIControl 안에 있고보기 A와보기 B가이 사용자 정의 UIControl의 두 개의 개별 인스턴스임을 알려야합니다. 두 개의 서로 다른 UIControl 사이에서이 정보를 전달하는 것이 바람직합니까? –

+0

죄송합니다. A를 의미하고 그것을 반영하기 위해 답을 변경했습니다. 두 객체가 하나의 객체에서 처리되면 아무 문제가 없습니다. 그런 다음 두 개의 경로를 정의하고 "A"하나의 형식 인 경우 "B"를 확인하십시오. 바로 지금, 그것이 "B"에 없을 때 아무 일도 일어나지 않습니다. – Tokuriku