在 UIScrollView 上添加 UIImageView,同时在 UITapGestureRecognizer 上添加 UIImageView
我有 UIViewController ,上面有 UIScrollView 。 UIScrollView有各种平铺图像,如Apple Developer平铺图像层程序。我正在尝试使用 UITapGestureRecognizer 来了解位置并在其顶部放置一个带有 uiimage 的 uiimageview 。但不知何故它永远不会放置图像文件。
- (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 Developer 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 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 Developer 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能错过了一些东西,但你的 imageView 为零。
您添加 [pagingScrollView addSubview:imageView] 但此时 imageView 尚未实例化。
由于您总是分配相同的图像,因此您应该这样做:
在“loadView”内部和
“viewDidUnload”内部
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:
inside of 'loadView' and
inside of 'viewDidUnload'