繁体   English   中英

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

[英]'Shrink' image size of UIButton when tapped

我有这个按钮(实例变量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];
    }

一种实现方法是将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);
}

然后,您可以像创建普通的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];
}

希望这可以帮助!

暂无
暂无

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

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