我的视图中有一个滚动条,在这个滚动条中有几个图像。
我想将这些图像拖放到我的滚动条中。我实现了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的经验以及可以响应此功能的图像数量吗?
我确实成功实现了两个手势识别器。所以我的滚动条中的图像现在可以拖动了。但是它限制在scrollview内部。我希望有一些功能可以将图像放在滚动视图的前面,这样我就可以将我的图像从滚动视图中拖出来。
在我实现的viewDidLoad方法中
[scroller addSubview:image1];
[scroller addSubview:image2];
[scroller addSubview:image3];
[[self view] addSubview:scroller];
在我的longPressed方法中我实现了
[image1 bringSubviewToFront:self.view];
有人有好的提示吗?非常感谢他们!
答案 0 :(得分:3)
添加滚动视图并创建一个IBOutlet
在此答案中为项目添加三个图像我使用名称 image1.png,image2.png和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个imageViews并添加一个手势识别器,代码更少,并告诉你imageView是longPressed
但是如果你想拖动imageView UILongPressGestureRecognizer对你不起作用,对于那个任务,你应该使用UIPanGestureRecognizer
如果要将imageView拖出scrollView,可以模仿该效果 从scrollView中删除/隐藏imageView并添加具有相同图像的imageView 您要隐藏/移除到要拖动imageView
的视图中的imageView的确切位置更多是视觉效果,但有效
答案 1 :(得分:0)
只需修改您的课程如下:
@implementation longGestureViewController
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
self.view = view;
self.view.tag = 100;
[view release];
mImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 10.0, 100.0, 120.0)];
mImageView1.image = [UIImage imageNamed:@"tile07.png"];
[self.view addSubview:mImageView1];
mImageView1.userInteractionEnabled = YES;
mImageView1.tag = 1;
[mImageView1 release];
mImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(120.0, 10.0, 100.0, 120.0)];
mImageView2.image = [UIImage imageNamed:@"tile08.png"];
mImageView2.userInteractionEnabled = YES;
mImageView2.tag = 2;
[self.view addSubview:mImageView2];
[mImageView2 release];
mImageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 140.0, 100.0, 120.0)];
mImageView3.image = [UIImage imageNamed:@"tile09.png"];
mImageView3.tag = 3;
mImageView3.userInteractionEnabled = YES;
[self.view addSubview:mImageView3];
[mImageView3 release];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
[mImageView1 addGestureRecognizer:longPress];
[longPress release];
longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
[mImageView2 addGestureRecognizer:longPress];
[longPress release];
longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
[mImageView3 addGestureRecognizer:longPress];
[longPress release];
}
-(void)longPressed:(UILongPressGestureRecognizer *)sender
{
CGPoint location = [sender locationInView:self.view];
UIView *view = [self.view hitTest:location withEvent:UIEventTypeTouches];
NSLog(@"%d", view.tag);
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc
{
[super dealloc];
}