2016-02-29 62 views
0

我在視圖中添加了一個子視圖,該視圖包含多個gesturerecognizer,但是當引入此視圖時,當用戶點擊這個新添加的視圖時,手勢不會觸發。這是我介紹的UIBezierPath但是。我讀過這可能是它不起作用的方式。我的問題是,是否可以使添加的UIBezierPath透明,以便設備會認爲正在挖掘繪製框下方的視圖。我希望我的意思是有道理的。gestureRecognizer無法在添加的子視圖

這是代碼爲我UIBezierPath

class drawRecognizerIndicator{ 
let gestureContainer: UIView! 
var view_gesture: UIView! 

init(container: UIView){ 
    gestureContainer = container 
} 

func drawRect(image: UIImage){ 
    let screenSize = gestureContainer.bounds 
    let screenWidth = screenSize.width 
    let screenHeight = screenSize.height 

    let gestureX = (screenWidth * 0.75) * 0.5 
    let gestureY = (screenWidth * 0.75) * 0.5 
    let gestureWidth = (screenWidth * 0.75) 
    let gestureHeight = (screenWidth * 0.75) 

    if(view_gesture != nil){ 
     if(view_gesture.isDescendantOfView(gestureContainer)){ 
      view_gesture.removeFromSuperview() 
     } 
    } 

    view_gesture = UIView(frame: CGRect(
     x: (screenWidth * 0.5) - gestureX, 
     y: (screenHeight * 0.5) - gestureY , 
     width: gestureWidth, 
     height: gestureHeight)) 
    view_gesture.backgroundColor = UIColor.blackColor() 
    view_gesture.alpha = 0 

    let maskPath = UIBezierPath(roundedRect: view_gesture.bounds, byRoundingCorners: .AllCorners, cornerRadii: CGSize(width: 10, height: 10)) 
    let maskLayer = CAShapeLayer(layer: maskPath) 
    maskLayer.frame = view_gesture.bounds 
    maskLayer.path = maskPath.CGPath 
    view_gesture.layer.mask = maskLayer 

    let view_gestureSize = view_gesture.bounds 
    let gestureIconWidth = view_gestureSize.width 
    let gestureIconHeight = view_gestureSize.height 

    let iconX = (gestureIconWidth * 0.8) * 0.5 
    let iconY = (gestureIconHeight * 0.8) * 0.5 
    let iconWidth = gestureIconWidth * 0.8 
    let iconHeight = gestureIconHeight * 0.8 

    let image_gesture = UIImageView(frame: CGRect(
     x: (gestureIconWidth * 0.5) - iconX, 
     y: (gestureIconHeight * 0.5) - iconY, 
     width: iconWidth, 
     height: iconHeight)) 
    image_gesture.contentMode = UIViewContentMode.ScaleAspectFit 
    image_gesture.image = image 
    image_gesture.userInteractionEnabled = true 
    view_gesture.addSubview(image_gesture) 
    view_gesture.userInteractionEnabled = true 
    gestureContainer.addSubview(view_gesture) 
    animateGestureNotification(view_gesture) 
} 

func animateGestureNotification(view_gesture: UIView){ 
    UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animations: { 
     view_gesture.alpha = 0.4 
     }, completion: nil) 

    UIView.animateWithDuration(0.5, delay: 2, options: UIViewAnimationOptions.CurveEaseOut, animations: { 
     view_gesture.alpha = 0 
     }, completion: nil) 
} 
} 
+0

你好,你檢查userInteractionEnabled =是 – Akash

+0

@Akash是的,我做到了。在drawable func的最後幾行代碼中,你可以找到它。 – NoSixties

回答

0

檢查

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, 
shouldRecognizeSimultaneouslyWithGestureRecognizer 
otherGestureRecognizer: UIGestureRecognizer) -> Bool {...} 

文檔它有助於決定何時有同樣的看法控制器上幾個手勢識別會發生什麼。

我會先將一些打印語句放在那裏看看究竟哪些GR被調用,然後決定哪些應該返回true。

+0

未調用drawrect時,手勢識別器可以正常工作。當你將圍繞將出現在視圖中的方框工作時,它們仍然有效。但是,當用戶在該框內使用相同的手勢時,它不會觸發手勢識別器。這是作爲持有手勢識別器的視圖的子視圖添加的。哪些已經以編程方式添加。希望能夠清除它 – NoSixties