使用所选属性播放/停止 UIButton 切换状态不起作用

发布于 2024-11-16 12:28:09 字数 1655 浏览 2 评论 0原文

我看不出我的代码有什么问题。当我按下按钮时什么也没有发生,就像默认状态甚至没有设置一样,这很奇怪,因为自 state 以来我们无法执行 playButton.state = UIControlStateNormal 的操作code> 属性是只读的。

这是我的代码:

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {

    // ...

    playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    playButton.frame = CGRectMake(100, 500, 100, 50);
    [playButton setTitle:@"play" forState: UIControlStateNormal];
    [playButton setTitle:@"stop" forState: UIControlStateSelected];
    playButton.exclusiveTouch = YES;
    playButton.selected = NO;
    [playButton addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];

- (void) play {

    if (playButton.state == UIControlStateNormal) {

        playButton.selected = YES;

        CABasicAnimation *maskAnim = [CABasicAnimation animationWithKeyPath:@"position.x"];
        maskAnim.byValue = [NSNumber numberWithFloat:diagramWidth];
        maskAnim.repeatCount = HUGE_VALF;
        maskAnim.duration = 1.750f;
        [self.maskLayer addAnimation:maskAnim forKey:@"position.x"];
        self.diagramLayer.mask = maskLayer;

        [backImageLayer addSublayer: self.diagramLayer];


        [audioPlayerNormal play];
        [moviePlayerNormal play]; 


    }
        else if (playButton.state == UIControlStateSelected) {

            playButton.selected = NO;

            [self.maskLayer removeAnimationForKey:@"position.x"];
            self.diagramLayer.mask = nil;

            [audioPlayerNormal stop];
            [moviePlayerNormal stop];

        }

}

I can't see what the problem is with my code. Nothing happens when I press the button, like as if the default state wasn't even set, which is weird because we can't do something playButton.state = UIControlStateNormal since the state property is read-only.

This is my code:

- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {

    // ...

    playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    playButton.frame = CGRectMake(100, 500, 100, 50);
    [playButton setTitle:@"play" forState: UIControlStateNormal];
    [playButton setTitle:@"stop" forState: UIControlStateSelected];
    playButton.exclusiveTouch = YES;
    playButton.selected = NO;
    [playButton addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];

and

- (void) play {

    if (playButton.state == UIControlStateNormal) {

        playButton.selected = YES;

        CABasicAnimation *maskAnim = [CABasicAnimation animationWithKeyPath:@"position.x"];
        maskAnim.byValue = [NSNumber numberWithFloat:diagramWidth];
        maskAnim.repeatCount = HUGE_VALF;
        maskAnim.duration = 1.750f;
        [self.maskLayer addAnimation:maskAnim forKey:@"position.x"];
        self.diagramLayer.mask = maskLayer;

        [backImageLayer addSublayer: self.diagramLayer];


        [audioPlayerNormal play];
        [moviePlayerNormal play]; 


    }
        else if (playButton.state == UIControlStateSelected) {

            playButton.selected = NO;

            [self.maskLayer removeAnimationForKey:@"position.x"];
            self.diagramLayer.mask = nil;

            [audioPlayerNormal stop];
            [moviePlayerNormal stop];

        }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2024-11-23 12:28:09

下面的代码应该可以完成这项工作。

- (void) play {

    if (playButton.selected == NO) {

        CABasicAnimation *maskAnim = [CABasicAnimation animationWithKeyPath:@"position.x"];
        maskAnim.byValue = [NSNumber numberWithFloat:diagramWidth];
        maskAnim.repeatCount = HUGE_VALF;
        maskAnim.duration = 1.750f;
        [self.maskLayer addAnimation:maskAnim forKey:@"position.x"];
        self.diagramLayer.mask = maskLayer;

        [backImageLayer addSublayer: self.diagramLayer];


        [audioPlayerNormal play];
        [moviePlayerNormal play]; 


    }
    else {
        [self.maskLayer removeAnimationForKey:@"position.x"];
        self.diagramLayer.mask = nil;

        [audioPlayerNormal stop];
        [moviePlayerNormal stop];

    }
    playButton.selected = !playButton.selected;
}

The following code should do the job.

- (void) play {

    if (playButton.selected == NO) {

        CABasicAnimation *maskAnim = [CABasicAnimation animationWithKeyPath:@"position.x"];
        maskAnim.byValue = [NSNumber numberWithFloat:diagramWidth];
        maskAnim.repeatCount = HUGE_VALF;
        maskAnim.duration = 1.750f;
        [self.maskLayer addAnimation:maskAnim forKey:@"position.x"];
        self.diagramLayer.mask = maskLayer;

        [backImageLayer addSublayer: self.diagramLayer];


        [audioPlayerNormal play];
        [moviePlayerNormal play]; 


    }
    else {
        [self.maskLayer removeAnimationForKey:@"position.x"];
        self.diagramLayer.mask = nil;

        [audioPlayerNormal stop];
        [moviePlayerNormal stop];

    }
    playButton.selected = !playButton.selected;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文