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

问题描述 投票:0回答:1

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

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

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

  • 图像摆动着
  • 图像进入另一个视图。
  • 图像跟随我的触摸位置

(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];  
    }
image uiscrollview drag
1个回答
0
投票

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

这是一个片段

首先,在viewDidLoad,viewDidAppear或viewWillappear方法中声明gesturerecognizer

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问题

© www.soinside.com 2019 - 2024. All rights reserved.