简体   繁体   English

如何在UIScrollView中移动UIImageView?

[英]How Can I Move An UIImageView Around In A UIScrollView?

In my application, I have a UIImageView inside a UIScrollView. 在我的应用程序中,我在UIScrollView中有一个UIImageView。 I want to zoom and drag the UIImageView. 我想缩放并拖动UIImageView。 I managed to do the zooming but I can't seem to find a way to drag the image inside the UIScrollView using touchesMoved. 我设法做缩放但我似乎找不到使用touchesMoved在UIScrollView中拖动图像的方法。 I have Googled around but it seems that the only method I have found was only possible in iOS 3.0+ but is now obsolete in iOS 4.0+. 我用谷歌搜索过,但似乎我发现的唯一方法只能在iOS 3.0+中使用,但现在在iOS 4.0+中已经过时了。 So how can I drag an image with my finger that is inside a UIScrollView? 那么如何用我的手指在UIScrollView中拖动图像呢?

You shouldn't need to do any touch detection yourself. 您不应该自己进行任何触摸检测。 UIScrollView can take care of everything. UIScrollView可以处理所有事情。

Here's a example: 这是一个例子:

- (void)viewDidLoad
{
    [super viewDidLoad];

    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.delegate = self;
    [self.view addSubview:scrollView];

    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.jpg"]];

    scrollView.contentSize = imageView.bounds.size;
    [scrollView addSubview:imageView];

    float minScale  = scrollView.frame.size.width  / imageView.frame.size.width;
    scrollView.minimumZoomScale = minScale;
    scrollView.maximumZoomScale = 2.0;
    scrollView.zoomScale = minScale;
}

Don't forget to add this delegate method to enable zooming: 不要忘记添加此委托方法以启用缩放:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imageView;    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何做链接到uiscrollview contentoffest的uiimageView缩放动画? - How i can do an uiimageView zoom animation linked to uiscrollview contentoffest? 如何在UIScrollView中找到UIImageView的缩放因子 - how can I find zooming factor of UIImageView inside UIScrollView 如何识别UIScrollView中的UIImageView子类上的轻击? - How can I recognize a tap on a UIImageView-subclass inside a UIScrollView? 如何在uiscrollview上像uiimageview一样调整uiview的大小? - How can I resize the uiview like that of uiimageview on uiscrollview? 如何在视图上添加触摸和检测UIImageView,这样我可以移动它而不是在它上面添加一个? - how to add on touch and detect UIImageView on view so i could move it around and not adding one on top of it? 如何在scrollViewDidZoom中动态调整contentInset?我最初在UIScrollview中设置了UIImageView的contentInset - How can I dynamically adjust contentInset in scrollViewDidZoom? I initially set contentInset of UIImageView in UIScrollview 如何检测用户是否只想滚动而不是在UIScrollView内点击UIImageView? - How can I detect if the user just wanted to scroll, and not tap on an UIImageView inside a UIScrollView? 如何在全屏 UIScrollView 中居中 UIImageView? - How do I center a UIImageView within a full-screen UIScrollView? 如何通过触摸和滑动来移动和滑出UIView? - How can I move around and slide away a UIView with touches and swipes? 如何以编程方式自动旋转UIScrollView和UIImageView? - How to autorotate UIScrollView and UIImageView programmatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM