I would like to implement the function of zooming the taped area with double tap, but I cannot zoom the UIImageView on scrollView.
(The reference site is here.)
http://cocoadays.blogspot.jp/2010/09/3.html
How do I zoom in on imageView?
-(void)viewDidLoad{
scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.frame =self.view.bounds;
scrollView.backgroundColor=[UIColor blueColor];
scrollView.conteimgViewntSize=CGSizeMake(imgView.bounds.size.width+100, imgView.bounds.size.height);
scrollView.frame = CGRectMake(0,50,320,500);
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.pagingEnabled = YES;
scrollView.boundsZoom=YES;
scrollView.minimumZoomScale=1.0;
scrollView.maximumZoomScale=2.0;
scrollView.showsHorizontalScrollIndicator=NO;
scrollView.showsVerticalScrollIndicator=NO;
[self.view addSubview:scrollView];
img=[UIImage imageNamed:[NSString stringWithFormat:@"a.jpg"]];
imgView = [[UIImageView alloc] initWithImage:img];
imgView.frame = CGRectMake(0,0,self.view.frame.size.width,448);
imgView.userInteractionEnabled=YES;
scrollView addSubview:imgView;
}
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
{
UITouch*touch=[touch anyObject];
if ([touch tapCount] == 2) {
scrollView=(UIScrollView*)self.view;
CGRect zoomRect;
if(scrollView.zoomScale>1.0){
zoomRect=scrollView.bounds;
} else{
zoomRect = [self zoomRectForScrollView:scrollView]
withScale: 2.0
withCenter: [touch locationInView:nil];
}
scrollView zoomToRect: zoomRect animated:YES;
}
}
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event
{
UITouch*touch=[touch anyObject];
if ([touch tapCount] == 2) {
// scrollView=(UIScrollView*)self.view;
// This line is wrong.
Corrected UIScrollView*scrollView=self.scrollView;//
CGRect zoomRect;
if(scrollView.zoomScale>1.0){
zoomRect=scrollView.bounds;
} else{
zoomRect = [self zoomRectForScrollView:scrollView]
withScale: 2.0
withCenter: [touch locationInView:nil];
}
scrollView zoomToRect: zoomRect animated:YES;
}
}
There may be other mistakes, but I have to make the above corrections to start talking.
The information on the link that was originally referenced is from 2010.I have to say that about five years ago is old.
The biggest problem is that the touchesEnded
method is used to retrieve tap events.UIScrollView
scrolls according to screen flicks and panes, so it does not propagate touch events from UIScrollView
through the Responder Chain.
The mainstream approach at this time is to retrieve double-tap events using the UITapGestureRecognizer
class.
Below is the sample code.Place 320x320 Scroll View with 320x320
Image View` and double tap to double and return to equal magnification.
ViewController.h
#import<UIKit/UIKit.h>
@ interface ViewController:UIViewController <UIScrollViewDelegate >
// Implementing the UIScrollViewDelegate Protocol Note 1
@end
ViewController.m
#import "ViewController.h"
@ interface ViewController()
@property(nonatomic)UIScrollView*scrollView;//ScrollView
@property(nonatomic) UIImageView* imageView; // ImageView
@end
@implementation ViewController
- (void) viewDidLoad{
superviewDidLoad;
self.scrollView=[[UIScrollView alloc] initWithFrame: CGRectMake (0.0, 40.0, 320.0, 320.0)];
// Generated a Tap Gesture Recognizer instance.
UITapGestureRecognizer*tapGestureRecognizer=[[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(doubleTap:)];
tapGestureRecognizer.numberOfTapsRequired=2;
// Respond only to double taps. (If you tap four times in a row, it will react twice.)
self.scrollView addGestureRecognizer:tapGestureRecognizer;
// Incorporate the Tap Gesture Recognizer into the Scroll View.
self.scrollView.delegate=self;//Note 1
self.scrollView.maximumZoomScale=2.0;
self.scrollView.minimumZoomScale=0.5;
// Set zoom limits
[self.view addSubview:self.scrollView];
self.imageView=[[UIImageView alloc] initWithImage:[UIImageimageName:@"image.png"]];
// Generate Image View
[self.scrollView addSubview:self.imageView];
}
// double-tap action method
- (void) doubleTap: (UITapGestureRecognizer*)recognizer{
// When the double tap is over,
if(recognizer.state==UIGestureRecognizerStateEnded){
// If the Scroll View has an equal magnification,
if(self.scrollView.zoomScale<1.5){
CGPoint tappedPoint= [recognizer locationInView:self.imageView];
// Obtain the tap position so that it expands around the tap position.
[self.scrollView zoomToRect:CGRectMake(tappedPoint.x/2.0, tappedPoint.y/2.0, 160.0, 160.0) animated:YES];
// enlargement
} If it's twice as much as else
self.scrollView zoomToRect:CGRectMake (0.0, 0.0, 320.0, 320.0) animated: YES;
// return to equal magnification
}
}
}
// Scroll View Delete Note 1
- (UIView*) viewForZoomingInScrollView: (UIScrollView*)scrollView{
return self.imageView;
}
@end
Note 1:
In UIScrollView
, to enable zoom, you must specify the UIView
(subclasses in ) to be zoomed in.To specify, use the Delete method viewForZoomingInScrollView:
.Therefore, you must configure the Delete destination for the ScrollView
.
I think the view controller should inherit the UIScrollViewDelegate and connect to the delete scroll view, and return the UIImageView, which is the content of the scroll view, with the viewForZoomingInScrollView: method.
Also, zooming in and out with a double tap might be more enjoyable when you want to use the UITapGestureRecognizer than writing in a touch event.
© 2024 OneMinuteCode. All rights reserved.