如何在iOS中点击缩放和双击缩小?
我正在开发一个应用程序,通过使用如何在iOS中点击缩放和双击缩小?,ios,objective-c,uiscrollview,uitapgesturerecognizer,Ios,Objective C,Uiscrollview,Uitapgesturerecognizer,我正在开发一个应用程序,通过使用UIScrollView来显示UIImages的图库,我的问题是,如何点击zoom并双击zoom,处理UIScrollView时,它是如何工作的?您需要在viewController中实现UITapgestureRecognitizer-docs - (void)viewDidLoad { [super viewDidLoad]; // what object is going to handle the gesture when
UIScrollView
来显示UIImages
的图库,我的问题是,如何点击zoom
并双击zoom
,处理UIScrollView
时,它是如何工作的?您需要在viewController中实现UITapgestureRecognitizer-docs
- (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];
}
基本上,这段代码是说,当在self.view
中注册uitappostation
时,将在self
中调用方法tapOnce或tapttweeds,具体取决于它是单次点击还是双次点击。因此,您需要将这些点击方法添加到您的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];
}
希望这对双击两次缩放的Swift 4.0版本有所帮助
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!
某处(通常在viewDidLoad中):
处理程序:
@objc 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))
}
}
非常感谢您的回答,我将测试它,并尽快报告。是否可能Webview识别触摸侧边Webview中的链接[tapOnce RequiremeTestureRecognitizerToFail:TapTworth];这正是我想要的。谢谢这在Swift 5/Xcode 12.4中仍然以同样的方式工作。要使用另一个双击(和动画)缩小,您只需将以下内容添加到
else
部分:scrollView.setZoomScale(scrollView.minimumZoomScale,动画:true)
@objc 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))
}
}