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

时间:2011-03-23 10:46:48

标签: iphone cocoa-touch uiscrollview subview uigesturerecognizer

在我的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负责移动图像。拖动仅在scrollview内部起作用。任何人都知道如何将我的图像带到视图前面?

非常感谢帮助

1 个答案:

答案 0 :(得分:0)

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

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

现在我希望能够将我的图像放回滚动视图中。我试图通过touchesended和doubletap实现这一点。图像将被放置在它的原点。但是它不会回到滚动条内部。这是一种触摸式方法。

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

希望有人可以指出我正确的方向,将图像放回到我的卷轴中,并放在它所在的同一位置。

提前致谢!

相关问题