UIScrollview 中的手势识别器

发布于 2024-10-24 08:07:39 字数 1806 浏览 7 评论 0原文

我的视图中有一个滚动条,并且该滚动条内有几张图像。

我想将这些图像拖放到(和放入)我的滚动条中。我实现了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏别ゝ 2024-10-31 08:07:39

添加滚动视图并创建一个 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 个 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

- (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);
}

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

慢慢从新开始 2024-10-31 08:07:39

只需修改您的课程,如下所示:

 @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];
}

Just modify your class like below:

 @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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文