在滚动视图内使用手势(通过手势将内容带出滚动视图)

发布于 2024-10-25 05:50:56 字数 1411 浏览 5 评论 0原文

在我的 UIScrollView 中我有一个视图。该视图中有几幅图像。我想将图像从滚动条(和视图)拖到主视图。然而,在滚动条和视图前面获取图像让我发疯。

这是我的一段代码:

- (void)viewDidLoad{

    //scroller properties
    scroller.contentSize = CGSizeMake(1300, 130);
    scroller.scrollEnabled = YES;
    scroller.directionalLockEnabled =YES;
    scroller.frame = CGRectMake(0, 874, 768, 130);
    [scroller setDelegate:self];

    UIView *contentContainer = [[UIView alloc] init];

    [scroller addSubview:contentContainer];
    [scroller addSubview:image1];
    [scroller bringSubviewToFront:image1];
    [speler1 release];

    UILongPressGestureRecognizer *longPress1 =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    [image1 addGestureRecognizer:longPress1];
    [longPress1 release];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]  initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [image1 addGestureRecognizer:panRecognizer];


}

-(void)longPressed:(UILongPressGestureRecognizer *)sender {

    [[self.view superview] bringSubviewToFront:image1];

}

-(void)move:(id)sender {

    //move the image

}

我想使用 LongPressGestureRecognizer 将图像置于视图前面。 PanGestureRecognizer 负责移动图像。拖动仅在滚动视图内有效。有人知道如何将我的图像带到视图前面吗?

非常感谢帮助

Inside my UIScrollView I've got a view. Within this view there are several images. I'd like to drag the images out of my scroller (and view) to the mainview. However getting the images in front of the scroller and view is driving me bonkers.

This is a piece of my code:

- (void)viewDidLoad{

    //scroller properties
    scroller.contentSize = CGSizeMake(1300, 130);
    scroller.scrollEnabled = YES;
    scroller.directionalLockEnabled =YES;
    scroller.frame = CGRectMake(0, 874, 768, 130);
    [scroller setDelegate:self];

    UIView *contentContainer = [[UIView alloc] init];

    [scroller addSubview:contentContainer];
    [scroller addSubview:image1];
    [scroller bringSubviewToFront:image1];
    [speler1 release];

    UILongPressGestureRecognizer *longPress1 =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    [image1 addGestureRecognizer:longPress1];
    [longPress1 release];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]  initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [image1 addGestureRecognizer:panRecognizer];


}

-(void)longPressed:(UILongPressGestureRecognizer *)sender {

    [[self.view superview] bringSubviewToFront:image1];

}

-(void)move:(id)sender {

    //move the image

}

I'd like to bring the images in front of the views by using a LongPressGestureRecognizer. The PanGestureRecognizer takes care of moving the image. The dragging works only inside the scrollview. Anyone knows how to bring my images in front of the views?

Help is greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

对风讲故事 2024-11-01 05:50:56

我成功地将图像从滚动条放到主视图中。请参阅下面的我的代码。

- (void)viewDidLoad{

    //scroller properties
    scroller.contentSize = CGSizeMake(1300, 130);
    scroller.scrollEnabled = YES;
    scroller.directionalLockEnabled =YES;
    scroller.frame = CGRectMake(0, 874, 768, 130);
    [scroller setDelegate:self];
    [scroller addSubview:image1];

    [image1 release];

    UILongPressGestureRecognizer *longPress1 =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    [image1 addGestureRecognizer:longPress1];
    [longPress1 release];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]  initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:2];
    [panRecognizer setMaximumNumberOfTouches:2];
    [panRecognizer setDelegate:self];
    [image1 addGestureRecognizer:panRecognizer];


}

-(void)longPressed:(UILongPressGestureRecognizer *)sender {

    CGPoint location = [sender locationInView:self.view];
    [self.view addSubview:image1];
    image1.center = location;
}

-(void)move:(id)sender {

    //move the image
}

现在我希望能够将图像放回滚动视图。我正在尝试通过 Touchsend 和双击来实现这一点。图像将被放置到其原点。但是它不会返回到滚动条内部。这是一个 TouchedEnd 方法的一部分。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDuration:0.20];
        [UIView commitAnimations];

        image1.transform = CGAffineTransformIdentity;
        image1.center = CGPointMake(20, 20);

        [self.view sendSubviewToBack:image1];

        return;
    }
}

希望有人能指出正确的方向,以便将图像放回滚动条中原来的位置。

提前致谢!

I succeeded with putting the image from the scroller to the main view. See my code below.

- (void)viewDidLoad{

    //scroller properties
    scroller.contentSize = CGSizeMake(1300, 130);
    scroller.scrollEnabled = YES;
    scroller.directionalLockEnabled =YES;
    scroller.frame = CGRectMake(0, 874, 768, 130);
    [scroller setDelegate:self];
    [scroller addSubview:image1];

    [image1 release];

    UILongPressGestureRecognizer *longPress1 =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    [image1 addGestureRecognizer:longPress1];
    [longPress1 release];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]  initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:2];
    [panRecognizer setMaximumNumberOfTouches:2];
    [panRecognizer setDelegate:self];
    [image1 addGestureRecognizer:panRecognizer];


}

-(void)longPressed:(UILongPressGestureRecognizer *)sender {

    CGPoint location = [sender locationInView:self.view];
    [self.view addSubview:image1];
    image1.center = location;
}

-(void)move:(id)sender {

    //move the image
}

Now I'd like to be able to put my images back to my scrollview. I'm trying to implement this by means of touchesended and a doubletap. The image will be placed to it's origin. However it won't go back inside the scroller. Here's a piece of the touchedended method.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        [UIView setAnimationDuration:0.20];
        [UIView commitAnimations];

        image1.transform = CGAffineTransformIdentity;
        image1.center = CGPointMake(20, 20);

        [self.view sendSubviewToBack:image1];

        return;
    }
}

Hope someone can point me in the right direction for putting the image back inside my scroller at the same location where it came from.

Thanks in advance!

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