2016-07-12 50 views
0

我想獲取用戶點擊的索引。我嘗試了幾種方法來檢測它,但它們都沒有工作。 這是我試過的。iCarousel獲取用戶點擊索引

func carousel(carousel: iCarousel, didSelectItemAtIndex index: Int) { 
     print("this has been tapped from Carousel \(index)") 
    } 

    func carouselCurrentItemIndexDidChange(carousel: iCarousel) { 
     let index=carousel.currentItemIndex 
     print("this is current index \(index) ") 
    } 

    func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView { 
     let webV:WKWebView! 
     webV = WKWebView(frame: CGRectMake(10, 10, 250, 250)) 
     if let updatedResource = myDict[index+1]{//name of resource I want to load 
      let url = NSBundle.mainBundle().URLForResource(updatedResource, withExtension: "html") 
      let requestObj = NSURLRequest(URL: url!); 
      webV.loadRequest(requestObj); 
      let tapGesture = UITapGestureRecognizer(target: self , action: Selector("tapDetected")) 
      //tapGesture.numberOfTapsRequired = 2 
      tapGesture.enabled = true 
      webV.userInteractionEnabled = true 
      webV.addGestureRecognizer(tapGesture) 

      // setUserInteractionEnabled = true 
      print("this has been rendered \(index)") 
      } 
     return webV 

    } 

    func tapDetected(sender:UISwipeGestureRecognizer) { 
     print("yes tap has been detected :)") 
+0

應該在'didSelectItemAtIndex'法檢測水龍頭。你是否設置了委託? – xmhafiz

+0

@ cod3rite,在設置委託後,Carousel顯示第一個或最後一個記錄。 – onkar

+0

是檢測到的龍頭? – xmhafiz

回答

0

也許沒有幫助,因爲我使用的Objective-C,但在

- (void)didTap:(UITapGestureRecognizer *)tapGesture 

方法得到檢測水龍頭。

[_delegate carousel:self didSelectItemAtIndex:index]; 
NSLog(@"Selected app at index: %d", index); 

按預期工作。我使用iCarousel版本1.8.2

0

斯威夫特

print(carousel.currentItemIndex)