UIScrollview 中的手势识别器
我的视图中有一个滚动条,并且该滚动条内有几张图像。
我想将这些图像拖放到(和放入)我的滚动条中。我实现了 LongPressGestureRecognizer。
我还想知道(通过代码)哪个图像被长按。我想我使用 CGRectContainsPoint 修复了最后一个问题。但是我的代码没有对 CGRectContainsPoint 做出反应。它仍然记录“失败”。
- (void)viewDidLoad{
//scroller properties
scroller.contentSize = CGSizeMake(1300, 130);
scroller.scrollEnabled = YES;
scroller.directionalLockEnabled =YES;
scroller.frame = CGRectMake(0, 874, 768, 130);
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
[image1 addGestureRecognizer:longPress];
[image2 addGestureRecognizer:longPress];
[image3 addGestureRecognizer:longPress];
[longPress release];
}
-(void)longPressed:(UILongPressGestureRecognizer *)sender {
CGPoint location = [sender locationInView:self.view];
if(CGRectContainsPoint(image1.frame, location)) {
NSLog(@"image1 has been longpressed");
// do something
}
if(CGRectContainsPoint(image2.frame, location)) {
NSLog(@"image2 has been longpressed");
// do something
}
if(CGRectContainsPoint(image3.frame, location)) {
NSLog(@"image3 has been longpressed");
// do something
}
else {
NSLog(@"fail");
}
有人有过这个 LongPressGestureRecognizer 的经验以及可以响应此功能的图像数量吗?
我确实成功地实现了两个手势识别器。所以我的滚动条中的图像现在可以拖动了。然而,它将自身限制在滚动视图内。我想要一些函数来负责将图像放在滚动视图前面,以便我可以将图像拖出滚动视图。
在我实现的 viewDidLoad 方法中
[scroller addSubview:image1];
[scroller addSubview:image2];
[scroller addSubview:image3];
[[self view] addSubview:scroller];
以及在我实现的 longPressed 方法中
[image1 bringSubviewToFront:self.view];
有人有好的提示吗?他们非常感激!
I've got a scroller within my view and got several images inside this scroller.
I'd like to drag and drop these images out (and into) my scroller. I implemented a LongPressGestureRecognizer.
I also would like to know (by code) which image got longpressed. I thought I fixed this last one by using CGRectContainsPoint. However my code is not reacting to the CGRectContainsPoint. It still Logs "fail".
- (void)viewDidLoad{
//scroller properties
scroller.contentSize = CGSizeMake(1300, 130);
scroller.scrollEnabled = YES;
scroller.directionalLockEnabled =YES;
scroller.frame = CGRectMake(0, 874, 768, 130);
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
[image1 addGestureRecognizer:longPress];
[image2 addGestureRecognizer:longPress];
[image3 addGestureRecognizer:longPress];
[longPress release];
}
-(void)longPressed:(UILongPressGestureRecognizer *)sender {
CGPoint location = [sender locationInView:self.view];
if(CGRectContainsPoint(image1.frame, location)) {
NSLog(@"image1 has been longpressed");
// do something
}
if(CGRectContainsPoint(image2.frame, location)) {
NSLog(@"image2 has been longpressed");
// do something
}
if(CGRectContainsPoint(image3.frame, location)) {
NSLog(@"image3 has been longpressed");
// do something
}
else {
NSLog(@"fail");
}
Has someone got experience with this LongPressGestureRecognizer and the number of images that can respond to this function?
i did succeeded implementing the two gesturerecognizers. So the images within my scroller are now draggable. However it limits itself inside the scrollview. I'd like to have some function which takes care of bringing the images in front of the scrollview, so that I can drag my images out of my scrollview.
Within my viewDidLoad method I implemented
[scroller addSubview:image1];
[scroller addSubview:image2];
[scroller addSubview:image3];
[[self view] addSubview:scroller];
and within my longPressed method I implemented
[image1 bringSubviewToFront:self.view];
Someone has good Tips? They are greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加滚动视图并创建一个 IBOutlet
将三张图像添加到您的项目中,在此答案中我使用名称
image1.png、image2.png 和 image3.png
最后是代码
通过这种方法,您将能够创建 3 个 imageView,并用更少的代码向它们添加一个手势识别器,并告诉您哪个 imageView 被长按了
但如果您想拖动 imageView UILongPressGestureRecognizer 不会为您工作,对于该任务,您应该使用 UIPanGestureRecognizer
如果您想将 imageView 拖出滚动视图,您可以模仿该效果
从scrollView中删除/隐藏imageView并添加具有相同图像的imageView
您隐藏/删除的 imageView 到底在哪里,您想要拖动 imageView 更像
是一种视觉效果,但有效
Add a scrollview and create one IBOutlet
Add three images to your project in this answer i use the names
image1.png, image2.png and image3.png
And finally here is the code
With this approach you will be able to create 3 imageViews and add to them a gesture recognizer with less code and tell you wich imageView was longPressed
But if you want to drag the imageView UILongPressGestureRecognizer is not going to work for you, for that task you should use UIPanGestureRecognizer
If you want to drag the imageView out of the scrollView you can mimic that effect
Removing/hiding the imageView from scrollView and add a imageView with the same image
exactly where was the imageView you hide/remove into the view you want to drag the imageView
Is more a visual effect but works
只需修改您的课程,如下所示:
Just modify your class like below: