将图像从滚动视图拖动到视图而不会影响其位置

时间:2011-03-04 15:24:27

标签: image uiscrollview drag

如何确保滚动条中的图像能够从滚动条拖动到图像视图?

我也想实现'摆动'。当用户在滚动条内轻击图像时,图像摆动并跟随用户的触摸到另一视图。我开始制作子视图,但是当图像在普通视图上显示时,它的位置会变为视图顶部而不是底部。

当我触摸图像时,如何确保:

  • 图像摆动
  • 图像转到另一个视图。
  • 图片跟随我的触摸位置

(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self.view];

    image1.userInteractionEnabled = YES;
    [self.view addSubview:image1];

    if([touch view] == image1){

        image1.center = location;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.14];
        [UIView setAnimationRepeatAutoreverses:YES];
        [UIView setAnimationRepeatCount:1000000];

        image1.transform = CGAffineTransformMakeRotation(69);
        image1.transform = CGAffineTransformMakeRotation(-69);

        [UIView commitAnimations];
        image1.center = CGPointMake(30,870);          
    }

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *) event {

    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:self.view];

    if([touch view] == image1){
        image1.userInteractionEnabled = YES;
        [self.view addSubview:image1];
        image1.center = location;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.14];
        [UIView setAnimationRepeatAutoreverses:YES];
        [UIView setAnimationRepeatCount:1000000];

        image1.transform = CGAffineTransformMakeRotation(69);
        image1.transform = CGAffineTransformMakeRotation(-69);

        [UIView commitAnimations];  
    }

1 个答案:

答案 0 :(得分:0)

我使用的是gestureRecognizers,而不是使用'touches functions'。您可以在特定图像上放置识别器(LongpressRecognizer的TapRecognizer)。当用户按下图像时,App识别触摸。之后,您可以使用“[self.view addSubview:yourImage]”函数将图像放在普通视图顶部的子视图中;“

这是一个snippit

首先在viewDidLoad,viewDidAppear或viewWillappear方法中声明手势识别器

UILongPressGestureRecognizer *longPress =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressed:)];
    longPress.minimumPressDuration = 0.7;
    [yourimage addGestureRecognizer:longPress1];

之后您使用委托功能

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

    CGPoint location = [sender locationInView:self.view];

    [self.view addSubview:yourimage];
    yourimage.center = location;
    //perform a transform (wiggle or scale)
    if([(UILongPressGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
        yourimage.transform = CGAffineTransformMakeScale(1.0, 1.0);
        //stop the transform

    }   
}

希望这可以帮助你解决Elppa