2015-04-21 8 views
5

मैं आईओएस विकास के लिए काफी नया हूं (इसलिए कृपया मेरी अक्षमता को माफ कर दो - मैंने हर जगह देखा है!), और CAShapeLayer पर टैप का पता लगाने का एक तरीका ढूंढ रहा था। अब तक, मैं hitTest पर आया हूं। hitTest सबसे अच्छी विधि है, और यदि ऐसा है तो स्विफ्ट में विशेष रूप से CAShapeLayer एस के साथ इसका उपयोग कैसे किया जाता है? इसके अलावा, अगर मेरे पास कई कैशपलेयर थे, तो मैं उन्हें अलग-अलग संदर्भित करने के लिए हिटटेस्ट विधि का उपयोग कैसे करूं?स्विफ्ट में एक कैशपलेयर पर टैप का पता लगाना?

यह मैं कैसे CAShapeLayer बनाया है: CAShapeLayer के स्पर्श प्राप्त के लिए कोड निम्नलिखित

let newBounds = CGRect(x: 0, y: 0, width: 200, height: 200) 
    let newShape = CAShapeLayer() 
    newShape.bounds = newBounds 
    newShape.position = view.center 
    newShape.cornerRadius = newBounds.width/2 
    newShape.path = UIBezierPath(ovalInRect: newShape.bounds).CGPath 

    newShape.lineWidth = 42 
    newShape.strokeColor = UIColor(red: 222/255.0, green: 171/255.0, blue: 66/255.0, alpha: 1.0).CGColor 
    newShape.fillColor = UIColor.clearColor().CGColor 

    newShape.strokeStart = 0.2 
    newShape.strokeEnd = 0.4 

    view.layer.addSublayer(newShape) 

उत्तर

7

यहाँ आप क्या हासिल करना चाहते करने के लिए सबसे अच्छा तरीका है imo:

// First add the shapelayer 
let layer = CAShapeLayer() 
layer.anchorPoint = CGPointZero 
layer.path = UIBezierPath(ovalInRect: CGRect(x: 0, y: 0, width: 100, height: 200)).CGPath 
layer.bounds = CGPathGetBoundingBox(layer.path) // IMPORTANT, without this hitTest wont work 
layer.fillColor = UIColor.redColor().CGColor 
self.view.layer.addSublayer(layer) 


// Check for touches 
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    let point = touches.anyObject()!.locationInView(self.view) // Where you pressed 

    if let layer = self.view.layer.hitTest(point) as? CAShapeLayer { // If you hit a layer and if its a Shapelayer 
     if CGPathContainsPoint(layer.path, nil, point, false) { // Optional, if you are inside its content path 
      println("Hit shapeLayer") // Do something 
     } 
    } 
} 
+0

मैंने अपना कोड आजमाया लेकिन कंसोल पर कुछ भी प्रिंट नहीं कर रहा है जब मैं किसी कारण से CAShapeLayer पर टैप करता हूं। – John

+0

मुझे दिखाएं कि आप उस आकार के निर्माता को कैसे बनाते हैं – Arbitur

+0

मैंने इसे ऊपर रखा है। – John

0

का प्रयोग करें।

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches) { 
     CGPoint touchLocation = [touch locationInView:self.view]; 
     for (id sublayer in self.view.layer.sublayers) { 
      BOOL touchInLayer = NO; 
      if ([sublayer isKindOfClass:[CAShapeLayer class]]) { 
       CAShapeLayer *shapeLayer = sublayer; 
       if (CGPathContainsPoint(shapeLayer.path, 0, touchLocation, YES)) { 
        // Here your code for do any opration. 
        touchInLayer = YES; 
       } 
      } else { 
       CALayer *layer = sublayer; 
       if (CGRectContainsPoint(layer.frame, touchLocation)) { 
        // Touch is in this rectangular layer 
        touchInLayer = YES; 
       } 
      } 
     } 
    } 
} 
+0

धन्यवाद। क्या आपको पता है कि क्या मैं स्विफ्ट में यह कोड प्राप्त कर सकता हूं? – John

+0

नहीं :-(कि –

+0

के लिए और अधिक अनुसंधान एवं विकास कार्य की जरूरत है मैं दृढ़ता से इस – Arbitur

1

स्विफ्ट 4,

मेरे UIImageView कई CAShapeLayer वस्तुओं चल रहा है, यहाँ मैं कैसे कर रहा था है उन पर नल का पता लगाने के लिए।

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

     guard let point = touch?.location(in: imageView) else { return } 
     guard let sublayers = imageView.layer.sublayers as? [CAShapeLayer] else { return } 

     for layer in sublayers{ 
      if let path = layer.path, path.contains(point) { 
       print(layer) 
      } 
     } 
    } 

Ref

0

मैं Arbitur के कोड का इस्तेमाल किया और मैं कुछ त्रुटियाँ थी। यहां एक कोड है जिसमें मुझे कोई त्रुटि नहीं है। स्विफ्ट 3.2/4.0

override func viewDidLoad() { 
    super.viewDidLoad() 

    let layer = CAShapeLayer() 
    layer.anchorPoint = CGPoint.zero 
    layer.path = UIBezierPath.init(ovalIn: CGRect(x: 0, y: 0, width: 100, height: 200)).cgPath 
    layer.bounds = (layer.path?.boundingBox)! // IMPORTANT, without this hitTest wont work 
    layer.fillColor = UIColor.red.cgColor 
    self.view.layer.addSublayer(layer) 

} 
    // Check for touches 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let point = touches.first?.location(in: self.view) // Where you pressed 

    if let layer = self.view.layer.hitTest(point!) as? CAShapeLayer { // If you hit a layer and if its a Shapelayer 
     if (layer.path?.contains(point!))! { // Optional, if you are inside its content path 
      print("Hit shapeLayer") // Do something 
     } 
    } 
} 
संबंधित मुद्दे