Add UIImageView on UIScrollView while UITapGestureRecognizer
I have UIViewController which has UIScrollView on it. The UIScrollView has various tile Images like Apple Developer tile image layer program. I am trying to use UITapGestureRecognizer to know the location and place a uiimageview with uiimage on top of it. But somehow it never places the image file.
- (void)loadView
{
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor clearColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.userInteractionEnabled = YES;
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
pagingScrollView.delegate = self;
self.view = pagingScrollView;
UIGestureRecognizer *recognizer;
recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
[pagingScrollView addGestureRecognizer:recognizer];
self.tapRecognizer = (UITapGestureRecognizer *)recognizer;
recognizer.delegate = self;
[recognizer release];
recycledPages = [[NSMutableSet alloc] init];
visiblePages = [[NSMutableSet alloc] init];
[self tilePages];
[pagingScrollView addSubview:imageView]; /// **** Here I am Adding to ScrollView *****//
}
- (void)showImageWithText:(NSString *)string atPoint:(CGPoint)centerPoint {
imageView.image = [UIImage imageNamed:@"image.png"];
imageView.center = centerPoint;
imageView.alpha = 1.0;
}
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:pagingScrollView];
int tapX = (int) location.x;
int tapY = (int) location.y;
[self showImageWithText:@"tap" atPoint:location];
}
//****More Code Same code like TilingLayer.m from Apple Develope开发者_如何学运维r Kit ****//
- (void)tilePages
{
// Calculate which pages are visible
CGRect visibleBounds = pagingScrollView.bounds;
int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
int lastNeededPageIndex = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
lastNeededPageIndex = MIN(lastNeededPageIndex, [self imageCount] - 1);
// Recycle no-longer-visible pages
for (ImageScrollView *page in visiblePages) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
[recycledPages addObject:page];
[page removeFromSuperview];
}
}
[visiblePages minusSet:recycledPages];
// add missing pages
for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
if (![self isDisplayingPageForIndex:index]) {
ImageScrollView *page = [self dequeueRecycledPage];
if (page == nil) {
page = [[[ImageScrollView alloc] init] autorelease];
}
[self configurePage:page forIndex:index];
[pagingScrollView addSubview:page];
[visiblePages addObject:page];
}
}
}
I am not sure what I am doing wrong here. If anyone has come across such an issue can share the ideas with me.
I may be missing something, but your imageView is nil.
You add [pagingScrollView addSubview:imageView] however imageView is not instantiated at this point.
Since you're always assigning the same image, you should just do:
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
inside of 'loadView' and
[imageView release];
imageView = nil;
inside of 'viewDidUnload'
精彩评论