UIScrollView + UIImageView放大/缩小

发布于 2024-11-26 13:50:54 字数 1526 浏览 2 评论 0原文

我有 UIScrollView 包含 3*UIImageView,每个 UIImageView 都有一个图像。 我使用的代码:

scrollView.delegate = self;

[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];

scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;

scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;

NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
  NSString *imageName = [NSString stringWithFormat:@"%d.jpg", (nimages + 1)];

  UIImage *image = [UIImage imageNamed:imageName];

  if (image == nil) {
    break;
  }

  imageView = [[UIImageView alloc] initWithImage:image];        
  CGRect rect = imageView.frame;
  rect.size.height = image.size.height;
  rect.size.width = image.size.width;
  rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
  rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
  [imageView setFrame:CGRectMake(cx, 0, self.view.frame.size.width, self.view.frame.size.height)];

  [scrollView addSubview:imageView];
  [imageView release];

  cx += scrollView.frame.size.width;
}

[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];

如何使用多点触控使 ScrollView 可缩放? 我使用了这段代码:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
} 

但它无法正常工作,图像重叠,因为 ScrollView 包含多个图像。

我该如何修复它? 谢谢

I have UIScrollView contains 3*UIImageView, each UIImageView has an image.
Code I used:

scrollView.delegate = self;

[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];

scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;

scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;

NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
  NSString *imageName = [NSString stringWithFormat:@"%d.jpg", (nimages + 1)];

  UIImage *image = [UIImage imageNamed:imageName];

  if (image == nil) {
    break;
  }

  imageView = [[UIImageView alloc] initWithImage:image];        
  CGRect rect = imageView.frame;
  rect.size.height = image.size.height;
  rect.size.width = image.size.width;
  rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
  rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
  [imageView setFrame:CGRectMake(cx, 0, self.view.frame.size.width, self.view.frame.size.height)];

  [scrollView addSubview:imageView];
  [imageView release];

  cx += scrollView.frame.size.width;
}

[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];

How can I make ScrollView zoom-able using multi-touch?
I used this code:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
} 

But it didn't work correctly, images got overlapped, because ScrollView contains multiple images.

How can I fix it?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

陌上芳菲 2024-12-03 13:50:54

不要直接将图像视图添加到滚动视图中,而是这样做:

为每个 UIImageView 单独创建一个新的 UIScrollView 对象,并将框架设置为与图像视图相同。将图像视图作为子视图添加到滚动视图中。请记住,这些滚动视图对象仅用于缩放图像,因此请设置zoomScale、minimumZoomScale 和maximumZoomScale,并设置滚动委托。在委托控制器中:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView.subViews objectAtIndex:0];
} 

然后,将所有滚动视图对象添加到您在代码中使用的滚动视图。该滚动视图仅用于滚动上面创建的子滚动视图。

明白了吗?

Don't add the image views to the scrollview directly, instead, do it this way:

Make a new UIScrollView object for each UIImageView separately and set the frame to be the same with the image view. Add the image view to the scrollview as a subview. Remember, these scroll view objects are used to zoom the images only, so set the zoomScale, minimumZoomScale and maximumZoomScale, also set the scroll delegate. In the delegate controller:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [scrollView.subViews objectAtIndex:0];
} 

Then, add all the scroll view objects to the scroll view you used in your code. This scroll view is used only to scroll the sub scrollviews created above.

Got the idea?

懒猫 2024-12-03 13:50:54

您可以在图像视图中使用 PinchGesture...
不需要使用 3 个滚动视图。

You can use PinchGesture to your image views...
There is no need of 3 scrollviews to be used.

暖风昔人 2024-12-03 13:50:54

尝试对您的 3 个图像进行此操作:

**.h Controller:**

@interface MyViewController : UIViewController{

IBOutlet UIScrollView *scrollView; 
UIImageView *imageView;

}

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;

.m 控制器:

@synthesize scrollView, imageView;


-(UIView *)viewForZoomingInScrollView:(UIScrollVi­ew *)scrollView{ 

return imageView;

}

- (void)viewDidLoad
{

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YourImageName.png"]]; 
self.imageView = tempImageView; 
[tempImageView release]; 
scrollView.maximumZoomScale = 3.0; 
scrollView.minimumZoomScale = 0.6; 
scrollView.clipsToBounds = YES; 
scrollView.delegate = self; 
[scrollView addSubview:imageView];
}

此代码对我有用:)

try this for your 3 images:

**.h Controller:**

@interface MyViewController : UIViewController{

IBOutlet UIScrollView *scrollView; 
UIImageView *imageView;

}

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;

.m Controller:

@synthesize scrollView, imageView;


-(UIView *)viewForZoomingInScrollView:(UIScrollVi­ew *)scrollView{ 

return imageView;

}

- (void)viewDidLoad
{

UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YourImageName.png"]]; 
self.imageView = tempImageView; 
[tempImageView release]; 
scrollView.maximumZoomScale = 3.0; 
scrollView.minimumZoomScale = 0.6; 
scrollView.clipsToBounds = YES; 
scrollView.delegate = self; 
[scrollView addSubview:imageView];
}

This code worked for me :)

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