2014-03-12 82 views
0

我有一個自定義UIScrollView基類SlideShow。這是一個水平滾動的幻燈片放映,其中包含可變數量的Slide視圖(來自UIImageView)。我在IB中的SlideShow對象上啓用了分頁,滾動和用戶交互。自定義UIScrollView的子視圖沒有獲取tap事件

我建立幻燈片放映這樣的:

@implementation SlideShow 
... 
// slides contains the pre-built Slide objects 
// The alpha and zulu stuff is for faking infinite scroll, when the user gets to an edge. 
- (void) configureSlides: (NSArray*) slides 
{ 
    CGSize scrollSize = CGSizeMake(0.0, 0.0); 
    self.zuluResetMarker = 0.0; 

    Slide* alphaSlide = [slides[0] copy]; 
    Slide* zuluSlide = [slides[slides.count - 1] copy]; 

    scrollSize = [self insertSlide: zuluSlide size: scrollSize]; 
    self.alphaSlideOffset = scrollSize.width; 
    for (Slide* s in slides) 
    { 
     scrollSize = [self insertSlide: s size: scrollSize]; 
    } 

    self.alphaResetMarker = scrollSize.width; 
    self.zuluSlideOffset = scrollSize.width - zuluSlide.frame.size.width; 
    scrollSize = [self insertSlide: alphaSlide size: scrollSize]; 
    self.contentSize = scrollSize; 

    [self setContentOffset: CGPointMake(self.alphaSlideOffset, 0.0) animated: NO]; 
} 

- (CGSize) insertSlide: (Slide*) slide size: (CGSize) aggregateSize 
{ 
    [slide setOriginX: aggregateSize.width originY: 0.0]; 
    CGSize s = slide.frame.size; 
    aggregateSize.height = s.height > aggregateSize.height ? s.height : aggregateSize.height; 
    aggregateSize.width += s.width; 
    [self addSubview: slide]; 
    return aggregateSize; 
} 

我設置UIGestureRecognizer的幻燈片來識別水龍頭,當我初始化:

@implementation Slide 
... 
- (id) initWithImage: (UIImage*) image delegate: (id<SlideDelegate>) delegate 
{ 
    if (self = [super initWithImage: image]) 
    { 
     self.delegate = delegate; 
     [self initialize]; 
    } 

    return self; 
} 

- (void) initialize 
{ 
    [self addGestureRecognizer: [[UITapGestureRecognizer alloc] 
           initWithTarget: self.delegate 
             action: @selector(slideTapped:)]]; 
} 

我驗證了initialize方法通過設置斷點來調用。但slideTapped:選擇器沒有。我將touchesBegan:withEvent:添加到SlideSlideShow類,並調用NSLog()SlideShow對象獲得呼叫,但Slide對象從來不會這樣做。 Slide對象是SlideShow的子視圖,根據我如何閱讀Apple的響應者鏈文檔,我預計Slide首先得到觸摸。

是否還有其他需要讓子視圖能夠響應觸摸事件?

+0

你有沒有嘗試設置 '目標' 上'addGestureRecognizer:initWithTarget:動作:''到的self'代替'自我。 delegate'? – Vinicius

+0

我試過了,它什麼也沒做。 – kwiqsilver

回答

2

問題是UIImageView實例將用戶交互默認爲NO。添加下面一行在您的init方法:self.userInteractionEnabled = YES;

你的情況:

- (void) initialize 
{ 
    self.userInteractionEnabled = YES; 
    [self addGestureRecognizer: [[UITapGestureRecognizer alloc] 
          initWithTarget: self 
            action: @selector(slideTapped:)]]; 
} 
相關問題