点击时“收缩”UIButton的图像大小

时间:2016-05-04 00:53:42

标签: ios objective-c uibutton

我有这个按钮(实例变量UIButton * _play),我希望它在点击时减小尺寸。因此,如果我按住手指按住按钮,我可以看到更改,然后它会发出新的呈现视图控制器的信号。我怎么做到这一点?

    - (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];
    }

1 个答案:

答案 0 :(得分:1)

一种方法是将UIButton子类化以创建自定义按钮。这只是一个实现“缩小”的例子。你想要的效果。
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);
}

然后,您可以像普通CustomButton

一样创建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];
}

希望这有帮助!