我有一個UIScrollView包含一個大的圖像大小爲3354 x 2082.我有UIScrollView縮小顯示整個圖像。我加入了紅色圓圈所示:UIScrollView在3倍變焦UIView覆蓋
CGRect positionFrame = CGRectMake(500,500,5,5);
RedCircle * circleView = [[RedCircle alloc] initWithFrame:positionFrame];
[self.scrollView addSubview:circleView];
問題 - 紅色圓圈不具有放大了同一位置(newZoomScale = 0.305307f)和全變焦(newZoomScale = 3.0F)。
這是比語法更合乎邏輯的問題。任何能幫助的天才?
編輯:發佈整個代碼:
@interface MainMap()
@property (nonatomic, strong) UIImageView *imageView;
- (void)centerScrollViewContents;
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer;
@end
@implementation MainMap
@synthesize scrollView = _scrollView;
@synthesize imageView = _imageView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
// Set up the image we want to scroll & zoom and add it to the scroll view
UIImage *image = [UIImage imageNamed:@"worldMap.jpg"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.scrollView addSubview:self.imageView];
// Tell the scroll view the size of the contents
self.scrollView.contentSize = image.size;
//self.scrollView.scrollEnabled = NO;
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[self.scrollView addGestureRecognizer:doubleTapRecognizer];
UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
twoFingerTapRecognizer.numberOfTapsRequired = 1;
twoFingerTapRecognizer.numberOfTouchesRequired = 2;
[self.scrollView addGestureRecognizer:twoFingerTapRecognizer];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// Set up the minimum & maximum zoom scales
CGRect scrollViewFrame = self.scrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width/self.scrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height/self.scrollView.contentSize.height;
CGFloat minScale = MIN(scaleWidth, scaleHeight);
self.scrollView.minimumZoomScale = minScale;
self.scrollView.maximumZoomScale = 3.0f;
self.scrollView.zoomScale = minScale;
[self centerScrollViewContents];
CGRect positionFrame = CGRectMake(500,500,5,5);
RedCircle * circleView = [[RedCircle alloc] initWithFrame:positionFrame];
[self.scrollView addSubview:circleView];
}
- (void)centerScrollViewContents{
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.imageView.frame;
if (contentsFrame.size.width < boundsSize.width) {
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2.0f;
} else {
contentsFrame.origin.x = 0.0f;
}
if (contentsFrame.size.height < boundsSize.height) {
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2.0f;
} else {
contentsFrame.origin.y = 0.0f;
}
self.imageView.frame = contentsFrame;
}
- (void)scrollViewDoubleTapped:(UITapGestureRecognizer*)recognizer{
// Get the location within the image view where we tapped
CGPoint pointInView = [recognizer locationInView:self.imageView];
// Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
CGFloat newZoomScale = 0.0f;
if(self.scrollView.zoomScale == 3.0f)
{
newZoomScale = 0.305307f;
} else
{
newZoomScale = 3.0f;
}
newZoomScale = MIN(newZoomScale, self.scrollView.maximumZoomScale);
// Figure out the rect we want to zoom to, then zoom to it
CGSize scrollViewSize = self.scrollView.bounds.size;
CGFloat w = scrollViewSize.width/newZoomScale;
CGFloat h = scrollViewSize.height/newZoomScale;
CGFloat x = pointInView.x - (w/2.0f);
CGFloat y = pointInView.y - (h/2.0f);
CGRect rectToZoomTo = CGRectMake(x, y, w, h);
[self.scrollView zoomToRect:rectToZoomTo animated:YES];
float scale = self.scrollView.zoomScale;
CGRect positionFrame = CGRectMake(500 * scale, 500 * scale ,50,50);
RedCircle * circleView = [[RedCircle alloc] initWithFrame:positionFrame];
[self.scrollView addSubview:circleView];
}
- (UIView*)viewForZoomingInScrollView:(UIScrollView *)scrollView{
// Return the view that we want to zoom
return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
// The scroll view has zoomed, so we need to re-center the contents
[self centerScrollViewContents];
}
- (void)viewDidUnload{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
不工作。我發佈了所有代碼供您參考。我確實在委託方法中添加了代碼,但沒有喜悅。 – sangony 2013-05-14 18:12:13
@sangony,哪部分不工作? – 2013-05-14 18:15:58
雙擊後紅色圓圈不在視圖中。 – sangony 2013-05-14 18:16:57