我有一个基于UIScrollView
的自定义课程SlideShow
。这是一个水平滚动幻灯片,包含可变数量的Slide
视图(源自UIImageView
)。我在IB中的SlideShow
对象上启用了分页,滚动和用户交互。
我按照以下方式制作幻灯片:
@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
首先获得触摸。
是否还需要其他东西才能让子视图能够响应触摸事件?
答案 0 :(得分:2)
问题是UIImageView实例默认用户与NO
的交互。在init方法中添加以下行:self.userInteractionEnabled = YES;
在你的情况下:
- (void) initialize
{
self.userInteractionEnabled = YES;
[self addGestureRecognizer: [[UITapGestureRecognizer alloc]
initWithTarget: self
action: @selector(slideTapped:)]];
}