在UIButton中实现动画 [英] Implement animation in UIButton

查看:139
本文介绍了在UIButton中实现动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是cocoa和iphone编程的新手,我想在UIButton中实现动画。例如,我创建了一个带有方形图像的自定义UIButton。然后当我按下那个UIButton时,方形图像会翻转。请注意,方形图像是UIButton的图像。

I'm new to cocoa and iphone programming and I want to implement an animation in UIButton. For example, I create a custom UIButton with a square image. Then when I press that UIButton, the square image will flip. Note that the square image is the image of the UIButton.

[UIButton setImage:[UIImage imageNamed:@"square.png"]];


推荐答案

以下是翻转按钮的完整代码(含它会被按下时的背景图片。
基本上你需要两个按钮和一个容器视图。

Below is the complete code for flipping a button (with background image) when it will pressed. Basically you need two button and a container view.

///// .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];

}

- (void) btn1_click
{

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:BarContainerView cache:YES];

[btn1 removeFromSuperview];
[BarContainerView addSubview:btn2];
[UIView commitAnimations];

}

- (void) btn2_click
{

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:BarContainerView cache:YES];

[btn2 removeFromSuperview];
[BarContainerView addSubview:btn1];
[UIView commitAnimations];

}

这篇关于在UIButton中实现动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆