亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在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 關注
  • 729 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號