为了账号安全,请及时绑定邮箱和手机立即绑定

如何在iOS中轻按以放大和双击以缩小?

如何在iOS中轻按以放大和双击以缩小?

慕仙森 2019-12-03 10:47:11
我正在开发一个应用程序,UIImages通过使用来显示的画廊UIScrollView,我的问题是,如何点按zoom并双击以zoom退出,使用处理时它是如何工作的UIScrollView。
查看完整描述

2 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

您需要实现UITapGestureRecognizer -文档在这里 -在你的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];


}

基本上,这段代码是说,UITapGesture在self.view方法中注册a时,将调用tapOnce或tapTwice,self具体取决于它是单击还是双击。因此,您需要将以下tap方法添加到您的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];

}

希望能有所帮助


查看完整回答
反对 回复 2019-12-03
?
慕斯王

TA贡献1864条经验 获得超2个赞

Swift 3.0版本,双击可放大两次。


@IBOutlet weak var scrollView: UIScrollView!

@IBOutlet weak var imageView: UIImageView!

某个地方(通常在viewDidLoad中):


let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onDoubleTap(gestureRecognizer:)))

tapRecognizer.numberOfTapsRequired = 2

scrollView.addGestureRecognizer(tapRecognizer)

处理程序:


func onDoubleTap(gestureRecognizer: UITapGestureRecognizer) {

    let scale = min(scrollView.zoomScale * 2, scrollView.maximumZoomScale)


    if scale != scrollView.zoomScale {

        let point = gestureRecognizer.location(in: imageView)


        let scrollSize = scrollView.frame.size

        let size = CGSize(width: scrollSize.width / scale,

                          height: scrollSize.height / scale)

        let origin = CGPoint(x: point.x - size.width / 2,

                             y: point.y - size.height / 2)

        scrollView.zoom(to:CGRect(origin: origin, size: size), animated: true)

        print(CGRect(origin: origin, size: size))

    }

}


查看完整回答
反对 回复 2019-12-03
  • 2 回答
  • 0 关注
  • 694 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信