简体   繁体   中英

UIView change frame when drag UIButton

I have problem. I have UIImageView with UIButton .

在此处输入图片说明

When I move UIButton as indicated by arrows, I need to change frame UIImageView of the button. And I need rotate UIImageView when I drag UIButton to top.

I think I need add Gesture in the UIButton. But How I can calculate new frame for the UIImageView , when I drag UIButton ?

Thank you

@synthesize resizableImageView;
@synthesize resizeButton;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [resizeButton addTarget:self action:@selector(startDraggableCorner) forControlEvents:UIControlEventTouchDown];
    [resizeButton addTarget:self action:@selector(stopDraggableCorner) forControlEvents:UIControlEventTouchUpInside];
}
-(void)startDraggableCorner
{
    gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(wasDragged:)];
    [[self view] addGestureRecognizer:gestureRecognizer];
}
-(void)wasDragged:(UIPanGestureRecognizer*)gesture
{
    CGRect imageFrame = [resizableImageView frame];
    imageFrame.size.height = [[gesture valueForKey:@"_lastScreenLocation"] CGPointValue].y - imageFrame.origin.y;
    imageFrame.size.width = [[gesture valueForKey:@"_lastScreenLocation"] CGPointValue].x - imageFrame.origin.x;
    [resizableImageView setFrame: imageFrame];
    CGRect buttonFrame = [resizeButton frame];
    buttonFrame.origin.x = [[gesture valueForKey:@"_lastScreenLocation"] CGPointValue].x;
    buttonFrame.origin.y = [[gesture valueForKey:@"_lastScreenLocation"] CGPointValue].y;
    [resizeButton setFrame:buttonFrame];

}
-(void)stopDraggableCorner
{
    [[self view] removeGestureRecognizer:gestureRecognizer];
}

This (almost) succeeds in your resizable behaviour. Rotating the image, perhaps you can check if [[gesture valueForKey:@"_lastScreenLocation"] CGPointValue].y is below a certain amount and then call a method to rotate the image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM