在 UIScrollView 上添加 UIImageView,同时在 UITapGestureRecognizer 上添加 UIImageView

发布于 2024-11-28 10:32:46 字数 3018 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上芳菲 2024-12-05 10:32:46

我可能错过了一些东西,但你的 imageView 为零。

您添加 [pagingScrollView addSubview:imageView] 但此时 imageView 尚未实例化。

由于您总是分配相同的图像,因此您应该这样做:

imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];

在“loadView”内部和

[imageView release];
imageView = nil;

“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:

imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];

inside of 'loadView' and

[imageView release];
imageView = nil;

inside of 'viewDidUnload'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文