i programmatically insert a custom botton (image) in xCode. After a click on the UIbutton i want to move the button to a custom place on the iPhone screen.
The programmatically button:
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
UIButton *MyButton = [UIButton buttonWithType:nil];
MyButton.frame = CGRectMake(100, 170, 100, 30);
[MyButton setImage:[UIImage imageNamed:@"tap.png"] forState:nil];
[self.view addSubview:MyButton];
[MyButton addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
i’m trying to move the button after a click.
-(void)buttonPressed {
NSLog(@"Button Pressed!");
CGRect frame = MyButton.frame;
frame.origin.x = 100; // new x coordinate
frame.origin.y = 4; // new y coordinate
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 0.1];
MyButton.frame = frame;
[UIView commitAnimations];
}
but nothing nothings happens.
I the .h file
IBOutlet UIButton *MyButton;
@property (nonatomic, retain) UIButton *MyButton;
It would be easier to use an
UIButton, because they already listen for tap events. Otherwise you will need to add anUIGestureRecognizerto your UIImageView. Once you have yourUIButtonset up in Interface Builder and connected to an IBOutlet, you can use this same code to move it.EDIT
Your problem may be that you are creating a new button instead of assigning to your MyButton property. Instead of:
try: