Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

objective c - How to tap to zoom and double tap to zoom out in iOS?

I'm developing an application to display a gallery of UIImages by using a UIScrollView, my question is, how to tap to zoom and double tap to zoom out, how does it work when handling with UIScrollView.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to implement UITapGestureRecognizer - docs here - in your viewController

- (void)viewDidLoad
{
    [super viewDidLoad];       

    // what object is going to handle the gesture when it gets recognised ?
    // the argument for tap is the gesture that caused this message to be sent
    UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce:)];
    UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwice:)];

    // set number of taps required
    tapOnce.numberOfTapsRequired = 1;
    tapTwice.numberOfTapsRequired = 2;

    // stops tapOnce from overriding tapTwice
    [tapOnce requireGestureRecognizerToFail:tapTwice];

    // now add the gesture recogniser to a view 
    // this will be the view that recognises the gesture  
    [self.view addGestureRecognizer:tapOnce];
    [self.view addGestureRecognizer:tapTwice];

}

Basically this code is saying that when a UITapGesture is registered in self.view the method tapOnce or tapTwice will be called in self depending on if its a single or double tap. You therefore need to add these tap methods to your UIViewController:

- (void)tapOnce:(UIGestureRecognizer *)gesture
{
    //on a single  tap, call zoomToRect in UIScrollView
    [self.myScrollView zoomToRect:rectToZoomInTo animated:NO];
}
- (void)tapTwice:(UIGestureRecognizer *)gesture
{
    //on a double tap, call zoomToRect in UIScrollView
    [self.myScrollView zoomToRect:rectToZoomOutTo animated:NO];
}

Hope that helps


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...