我有一个按钮,我想用它来打开/关闭视图...
它是一个箭头,打开时箭头会旋转 180°。
并且在关闭时,它应该向后旋转 180°。
但是,它在打开时只旋转一次,但在关闭时不会再次旋转。此外,如果我第二次打开它,即使第一次打开它也不会再次旋转。
这是代码:
- (id)initWithFrame:(CGRect)frame
{
mediaOpened = NO;
media = [UIButton buttonWithType:UIButtonTypeCustom];
[media retain];
[media setBackgroundImage:[ImageController getImageNamed:@"arrow.png"] forState:UIControlStateNormal];
[media addTarget:self action:@selector(toggleMedia) forControlEvents:UIControlEventTouchUpInside];
media.frame = CGRectMake(2, 10, 26, 17);
[self addSubview:media];
}
-(void) rotateMedia {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
float angle = M_PI;
media.transform = CGAffineTransformMakeRotation(angle);
[UIView commitAnimations];
}
-(void) mediaClicked {
if (self.delegate != nil)
[self.delegate mediaClicked];
[self rotateMedia];
}
-(void) mediaUnclicked {
if (self.delegate != nil)
[self.delegate mediaUnClicked];
[self rotateMedia];
}
-(void) toggleMedia {
if (!mediaOpened)
[self mediaClicked];
else
[self mediaUnclicked];
mediaOpened = !mediaOpened;
}