【问题标题】:Drag image from scrollview to view without compromising its location将图像从滚动视图拖动到视图而不影响其位置
【发布时间】:2011-03-04 15:24:27
【问题描述】:

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

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

我怎样才能确保当我触摸图像时:

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

(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】:

    我没有使用“触摸功能”,而是使用了手势识别器。您可以在某个图像上放置一个识别器(LongpressRecognizer 的 TapRecognizer)。当用户按下图像时,应用程序会识别触摸。之后,您可以使用函数“[self.view addSubview:yourImage];”将图像放在普通视图之上的子视图中

    这里有一个片段

    首先在 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多