简体   繁体   中英

'Shrink' image size of UIButton when tapped

I have this button (instance variable UIButton* _play) and I want it to decrease in size when tapped. So if I tap and hold my finger on the button, I can see the change before it signals the new presented view controller to be loaded. How do I get that to happen?

    - (void)viewDidLoad {
        _play = [UIButton playButtonCreate];
        [_play addTarget:self
                  action:@selector(playButton:)
        forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_play];
        _play.frame = CGRectMake(107.5, 230, 105, 105);
    }

    - (IBAction)playButton:(id)sender {
          PlayViewController* obj = [PlayViewController new];
          obj.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
          [self presentViewController:obj animated:YES completion:nil];
          [self performSelector:@selector(setUpRockTitles) withObject:nil afterDelay:0.5];
    }

One way to do it is to subclass UIButton to create your custom button. Here is just an example to achieve the 'shrink' effect you want.
In the CustomButton.m file:

@implementation CustomButton

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.transform = CGAffineTransformMakeScale(0.8, 0.8); // set your own scale
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.transform = CGAffineTransformMakeScale(1.0, 1.0);
    [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.transform = CGAffineTransformMakeScale(1.0, 1.0);
}

Then you can create the CustomButton just like a normal UIButton :

 - (void)viewDidLoad {
    _play = [[CustomButton alloc] initWithFrame:CGRectMake(107.5, 230, 105, 105)];
    [_play addTarget:self
              action:@selector(playButton:)
    forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_play];
}

Hope this helps!

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