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:
添加到Slide
和SlideShow
類,並調用NSLog()
。 SlideShow
對象獲得呼叫,但Slide
對象從來不會這樣做。 Slide
對象是SlideShow
的子視圖,根據我如何閱讀Apple的響應者鏈文檔,我預計Slide
首先得到觸摸。
是否還有其他需要讓子視圖能夠響應觸摸事件?
你有沒有嘗試設置 '目標' 上'addGestureRecognizer:initWithTarget:動作:''到的self'代替'自我。 delegate'? – Vinicius
我試過了,它什麼也沒做。 – kwiqsilver