Change the position of the UIView / UIControl screen when you click on it

I know well that this is a simple question. I would like to learn how to move CGRect / UIButton to another place on the screen when the user selects it. Thank you for your help.

- (void)showMeSomeButtons:(CGRect)frame{ button = [[UIButton alloc] initWithFrame:frame]; button.frame = CGRectMake(240, 150, 50, 50); UIImage *buttonGraphic = [UIImage imageNamed:@"1.png"]; [button setBackgroundImage:buttonGraphic forState:UIControlStateNormal]; [button addTarget:self.viewController action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; } -(void)buttonClicked{ ??? } 
+4
source share
2 answers

I think you need to add : to the @selector(buttonClicked:) operator, since IBAction expects one attribute of the (id)sender method.

You can do something similar to the code below.

 - (void)buttonClicked:(id)sender { CGRect frame = button.frame; frame.origin.x = 500; // new x coordinate frame.origin.y = 500; // new y coordinate button.frame = frame; } 

If you want to animate it, you can add a beginAnimation block.

 - (void)buttonClicked:(id)sender { CGRect frame = button.frame; frame.origin.x = 500; // new x coordinate frame.origin.y = 500; // new y coordinate [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration: 0.25]; button.frame = frame; [UIView commitAnimations]; } 
+14
source

CGRectOffset(rect, dx, dy)

Translates the beginning of the rectangle by delta X and Y.

+1
source

Source: https://habr.com/ru/post/1336238/


All Articles