Iphone 在UIButton中实现动画
我是cocoa和iphone编程新手,我想在UIButton中实现一个动画。例如,我创建了一个带有方形图像的自定义UIButton。然后,当我按下那个按钮时,方形图像将翻转。请注意,方形图像是UIButton的图像Iphone 在UIButton中实现动画,iphone,Iphone,我是cocoa和iphone编程新手,我想在UIButton中实现一个动画。例如,我创建了一个带有方形图像的自定义UIButton。然后,当我按下那个按钮时,方形图像将翻转。请注意,方形图像是UIButton的图像 [UIButton setImage:[UIImage imageNamed:@"square.png"]]; 下面是按下按钮时翻转按钮(带有背景图像)的完整代码。 基本上,您需要两个按钮和一个容器视图 ///// .H file code.... //Containe
[UIButton setImage:[UIImage imageNamed:@"square.png"]];
下面是按下按钮时翻转按钮(带有背景图像)的完整代码。 基本上,您需要两个按钮和一个容器视图
///// .H file code....
//Container views used for flipping the bars to show whose turn it is
UIButton* btn1;
UIButton* btn2;
UIView *BarContainerView;
///// .M file code....
- (void)viewWillAppear:(BOOL)animated
{
BarContainerView = [[UIView alloc] initWithFrame:CGRectMake(20, 30, 103, 150)];
btn1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
[btn1 addTarget:self action:@selector(btn1_click) forControlEvents:UIControlEventTouchUpInside];
[btn1 setBackgroundImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal];
btn1.frame = CGRectMake(0, 0, 103, 150);
btn2 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
[btn2 addTarget:self action:@selector(btn2_click) forControlEvents:UIControlEventTouchUpInside];
[btn2 setBackgroundImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal];
btn2.frame = CGRectMake(0, 0, 103, 150);
[BarContainerView addSubview:btn1];
[self.view addSubview:BarContainerView];
[self.view bringSubviewToFront:BarContainerView];
}
我也有同样的问题,我用一种非常相似的方式解决了它。我只是对UIButton进行了子类化,并实现了如下方法:
- (void) flipBackgroundImage:(UIImage*) image
{
[UIView beginAnimations:@"flipbutton" context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
[self setBackgroundImage:image forState:UIControlStateNormal];
[UIView commitAnimations];
}
工作起来很有魅力
干杯,
安卡
- (void) flipBackgroundImage:(UIImage*) image
{
[UIView beginAnimations:@"flipbutton" context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
[self setBackgroundImage:image forState:UIControlStateNormal];
[UIView commitAnimations];
}