Iphone 使用选定属性为“状态不工作”切换播放/停止UIButton

Iphone 使用选定属性为“状态不工作”切换播放/停止UIButton,iphone,objective-c,xcode,uibutton,state,Iphone,Objective C,Xcode,Uibutton,State,我看不出我的代码有什么问题。当我按下按钮时,没有发生任何事情,就像默认状态甚至没有设置一样,这很奇怪,因为我们无法执行某些操作playButton.state=uicontrolstateformal,因为state属性是只读的 这是我的代码: - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // ... playButton = [UIButton but

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

这是我的代码:

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

的确如此!非常感谢你。但有一点我不明白:像我正在做的那样检查if语句中控件的状态有什么问题?甚至我都不知道。尝试在其操作方法中记录按钮状态。它说按钮处于突出显示状态。我猜它会一直保持高亮显示状态,直到它从action方法返回。检查了相同的内容。在某种意义上说,当它运行动作方法时,它会保持高亮显示,以防你想在手指仍被按下时显示一些东西。无论如何,再次感谢你的帮助。干杯
- (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;
}