Swift UIImageView缩放和触摸图像
我有一个大图像,我想在ScrollView中显示这个图像,我想用图像的x和y坐标检测触摸区域 如果我选择ScrollView的“用户交互启用”,zoom正在工作,但ToucheSBegined方法在ScrollView上不工作(它在外部工作), 如果我没有选择ScrollView的“已启用用户交互”,缩放不起作用,但触摸开始在ScrollView上使用方法,我得到了坐标,但无法缩放 我怎样才能解决这个问题 代码: 类ViewController:UIViewController、UIScrollViewDelegate{Swift UIImageView缩放和触摸图像,swift,uiscrollview,swift3,swift2,Swift,Uiscrollview,Swift3,Swift2,我有一个大图像,我想在ScrollView中显示这个图像,我想用图像的x和y坐标检测触摸区域 如果我选择ScrollView的“用户交互启用”,zoom正在工作,但ToucheSBegined方法在ScrollView上不工作(它在外部工作), 如果我没有选择ScrollView的“已启用用户交互”,缩放不起作用,但触摸开始在ScrollView上使用方法,我得到了坐标,但无法缩放 我怎样才能解决这个问题 代码: 类ViewController:UIViewController、UIScroll
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
self.scrollView.minimumZoomScale=1.0;
self.scrollView.isScrollEnabled = true
self.scrollView.maximumZoomScale=7.0;
self.scrollView.contentSize=CGSize(width: 1424,height: 1410);
self.scrollView.delegate=self;
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return self.imageView
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let position = touch.location(in: self.imageView)
print(position.x)
print(position.y)
}
}
@IBMOutlet弱var scrollView:UIScrollView!
@IBVAR imageView:UIImageView!
重写func viewDidLoad(){
self.scrollView.minimumZoomScale=1.0;
self.scrollView.isScrollEnabled=true
self.scrollView.maximumZoomScale=7.0;
self.scrollView.contentSize=CGSize(宽:1424,高:1410);
self.scrollView.delegate=self;
}
func VIEWFOR缩放(在scrollView:UIScrollView中)->UIView{
返回self.imageView
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
如果让触摸=先触摸{
让位置=触摸位置(在:self.imageView中)
打印(位置x)
打印(位置y)
}
}
Hey BadCode你解决了你的问题吗?我在这里也遇到了同样的问题。嗨,是的,我解决了这个问题。首先,你应该在XCode中的视图中为View和ScrollView启用用户交互。另外,我添加了TapGestureRecognizer,并在viewDidLoad()中添加了这些代码。//开始代码myGestureRecognizer=UITapGestureRecognizer(目标:self,action:#选择器(self.tapGestureExportAction(35;:))imageView.AddGestureRecognitizer(MyGestureRecognitizer)imageView.isUserInteractionEnabled=true//代码结束。我创建了一个名为tapGestureExportAction的函数。谢谢老兄,我会按照你的建议尝试它。