UIView drag (image and text)

前端 未结 6 1915
悲&欢浪女
悲&欢浪女 2020-11-27 13:59

Is it possible to drag UIView around the iOS screen while it has both image and text? e.g. small cards. Could you point me to the similar (solved) topic? I haven\'t found an

6条回答
  •  天命终不由人
    2020-11-27 14:48

    An addition to MHC's answer.

    If you don't want the upper left corner of the view to jump under your finger, you can also override touchesBegan like this:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *aTouch = [touches anyObject];
    
        offset = [aTouch locationInView: self];
    }
    

    and change MHC's touchesMoved to:

     -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
     {
          UITouch *aTouch = [touches anyObject];
          CGPoint location = [aTouch locationInView:self.superview];
    
          [UIView beginAnimations:@"Dragging A DraggableView" context:nil];
          self.frame = CGRectMake(location.x-offset.x, location.y-offset.y, 
                                  self.frame.size.width, self.frame.size.height);
          [UIView commitAnimations];
      }
    

    you should also define CGPoint offset in the interface:

    @interface DraggableView : UIView
    {
        CGPoint offset;
    }
    

    EDIT:

    Arie Litovsky provides more elegant solution that allows you to ditch the ivar: https://stackoverflow.com/a/10378382/653513

提交回复
热议问题