我有一个包含大小为3354 x 2082的大图像的UIScrollView。我将UIScrollView缩小以显示整个图像。我正在添加这样的红色圆圈:
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
答案 0 :(得分:1)
使用UIScrollView的zoomScale属性。
float scale = self.scrollView.zoomScale;
CGRect positionFrame = CGRectMake(500 * scale, 500 * scale ,5,5);
在scrollViewDidZoom:
委托方法中调整位置。
答案 1 :(得分:0)
而不是
[self.scrollView addSubview:circleView];
我应该
[self.imageView addSubview:circleView];
这解决了我所有的问题。