简体   繁体   English

轻按时UIButton的“缩小”图像大小

[英]'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. 我有这个按钮(实例变量UIButton * _play),我希望它在点击时减小尺寸。 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. 一种实现方法是将UIButton子类化以创建自定义按钮。 Here is just an example to achieve the 'shrink' effect you want. 这只是一个实现所需“缩小”效果的示例。
In the CustomButton.m file: CustomButton.m文件中:

@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 : 然后,您可以像创建普通的UIButton一样创建CustomButton

 - (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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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