0

我的UIViewController擁有的UIScrollView上添加的UIImageView。 UIScrollView具有各種平鋪圖像,如Apple Developer平鋪圖像圖層程序。我正在嘗試使用UITapGestureRecognizer來知道位置,並將uiimageview放置在它的頂部。但不知何故,它永遠不會放置圖像文件。上的UIScrollView而UITapGestureRecognizer

- (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]; 
    } 
}  

}

我不知道我在做什麼錯在這裏。如果有人遇到過這樣的問題,可以與我分享想法。

+0

做你的showImageWithText方法被調用? imageView創建/定義在哪裏? – ader

+0

是的。它被稱爲,但我想我忘了創建和定義imageView。 – lifemoveson

+0

如MALAXEUR說,可使用:ImageView的= [[ALLOC的UIImageView] initWithImage:[UIImage的imageNamed:@ 「image.png」]];然後將其添加到scrollView。或者甚至在頭文件中定義UIImageView。 – ader

回答

1

我可能失去了一些東西,但你的ImageView的是零。

您添加[pagingScrollView addSubview:ImageView的]但是ImageView的是不是在這一點上實例化。

因爲你總是分配相同的圖像,你應該只是做:

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

'的loadView' 和

[imageView release]; 
imageView = nil; 

內的內部 'viewDidUnload'

+0

我曾嘗試創建UIImageView的實例,但沒有幫助。我添加了更多的代碼,以便讓它更有意義,我希望。 – lifemoveson

相關問題