Domanda

I am new to iphone i am working on project in which i am autoscrolling the image with scrollview also along with it i have to do zoom in and out of image.Is this possible ? and if Yes how it can be done.Please suggest me some code.

Thanks.

È stato utile?

Soluzione

- (void)viewDidLoad
{
 [super viewDidLoad];

// 1
   UIImage *image = [UIImage imageNamed:@"photo1.png"];
   self.imageView = [[UIImageView alloc] initWithImage:image];
   self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
  [self.scrollView addSubview:self.imageView];

// 2
   self.scrollView.contentSize = image.size; 
  }

viewDidLoad

  1. First, you need to create an image view with the photo1.png image you added to your project and you set the image view frame (it’s size and position) so it’s the size of the image and sits at point 0,0 within the parent. Finally, the image view gets added as a subview of your scroll view.

  2. You have to tell your scroll view the size of the content contained within it, so that it knows how far it can scroll horizontally and vertically. In this case, it’s the size of the image.

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

// 1
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;

// 2
self.scrollView.maximumZoomScale = 1.0f;
self.scrollView.zoomScale = minScale;

}

viewWillAppear

  1. Next, you need to work out the minimum zoom scale for the scroll view. A zoom scale of one means that the content is displayed at normal size. A zoom scale below one shows the content zoomed out, while a zoom scale of greater than one shows the content zoomed in.

  2. You set the maximum zoom scale as 1, because zooming in more than the image’s resolution can support will cause it to look blurry. You set the initial zoom scale to be the minimum, so that the image starts fully zoomed out.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top