使用所选属性播放/停止 UIButton 切换状态不起作用
我看不出我的代码有什么问题。当我按下按钮时什么也没有发生,就像默认状态甚至没有设置一样,这很奇怪,因为自 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的代码应该可以完成这项工作。
The following code should do the job.