问题描述
我的视图中有一个滚动条,在这个滚动条中有几个图像。
I've got a scroller within my view and got several images inside this scroller.
我想将这些图像拖放到(并进入)我的卷轴。我实现了LongPressGestureRecognizer。
I'd like to drag and drop these images out (and into) my scroller. I implemented a LongPressGestureRecognizer.
我也想知道(通过代码)哪个图像被长按了。我以为我使用CGRectContainsPoint修复了最后一个。但是我的代码没有对CGRectContainsPoint做出反应。它仍然记录失败。
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");
}
有人有使用此LongPressGestureRecognizer的经验以及可以响应的图像数量这个功能?
Has someone got experience with this LongPressGestureRecognizer and the number of images that can respond to this function?
我确实成功实现了两个手势识别器。所以我的滚动条中的图像现在可以拖动了。但是它限制在scrollview内部。我希望有一些功能可以将图像放在滚动视图的前面,这样我就可以将我的图像从滚动视图中拖出来。
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.
在我的内部我实现的viewDidLoad方法
Within my viewDidLoad method I implemented
[scroller addSubview:image1];
[scroller addSubview:image2];
[scroller addSubview:image3];
[[self view] addSubview:scroller];
在我实施的longPressed方法中
and within my longPressed method I implemented
[image1 bringSubviewToFront:self.view];
有人有好的提示吗?非常感谢他们!
Someone has good Tips? They are greatly appreciated!
推荐答案
添加一个scrollview并创建一个IBOutlet
Add a scrollview and create one IBOutlet
在这个答案中为你的项目添加三个图像我使用名称
image1.png,image2.png和image3.png
Add three images to your project in this answer i use the namesimage1.png, image2.png and image3.png
最后这里是代码
- (void) viewDidLoad{
[super viewDidLoad];
for (int i=1; i<=3; i++) {
NSString *file = [NSString stringWithFormat:@"image%d.png", i];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:file]];
imageView.frame = CGRectMake(i * 200, 0, 200, 200);
imageView.userInteractionEnabled = YES;
imageView.tag = i;
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[thumbView addGestureRecognizer:recognizer];
[scrollView addSubview:thumbView];
}
}
-(void)longPress: (UILongPressGestureRecognizer *)recognizer
{
UIView *view = recognizer.view;
int index = view.tag;
NSLog(@"image pressed : %i", índex);
}
使用这种方法,您将能够创建3个imageView并添加一个手势识别器用更少的代码告诉你图像是longPressed
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
但如果你想拖动imageView UILongPressGestureRecognizer对你不起作用,你应该使用该任务UIPanGestureRecognizer
But if you want to drag the imageView UILongPressGestureRecognizer is not going to work for you, for that task you should use UIPanGestureRecognizer
如果要将imageView拖出scrollView,可以模仿该效果
从scrollView中删除/隐藏imageView并添加带有相同效果的imageView image
究竟在哪里是你隐藏/删除你想要拖动imageView的视图中的imageView
If you want to drag the imageView out of the scrollView you can mimic that effectRemoving/hiding the imageView from scrollView and add a imageView with the same imageexactly where was the imageView you hide/remove into the view you want to drag the imageView
更多是视觉效果但是有效
Is more a visual effect but works
这篇关于UIScrollview中的手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!