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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将图像从滚动视图拖动到视图而不会影响其位置相关的知识,希望对你有一定的参考价值。

如何确保滚动条中的图像能够从滚动条拖动到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];  
    }
答案

我没有使用'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问题

以上是关于将图像从滚动视图拖动到视图而不会影响其位置的主要内容,如果未能解决你的问题,请参考以下文章

如何将 UIImage 从滚动视图拖动到视图

如何将Listview数据裁剪到android中的滚动位置?

Android从地图片段底部拖动回收器视图

UIScrollView 下拉父视图

从 ipad 故事板中的视图控制器控制拖动到工具栏而不是自动滚动故事板

滚动视图无法执行委托,但表视图可以