UIButton 与 UIImageView 子视图显示动画图像
我正在尝试创建一个显示动画图像的按钮。我在this帖子中读到我添加 UIImageView (带有animationImages数组)作为子视图的建议UIButton 的。我尝试这样做: (animation1 是标头中声明的 UIImageView 对象)
- (void)viewDidLoad {
UIImage *image1 = [UIImage imageNamed:@"Good1.png"];
UIImage *image2 = [UIImage imageNamed:@"Good2.png"];
UIImage *image3 = [UIImage imageNamed:@"Bad1.png"];
NSArray *images = [[NSArray alloc] initWithObjects:image1, image2, image3, nil];
animation1.animationImages = images;
[images release];
[self.button1 addSubview:animation1];
[self.button1 bringSubviewToFront:animation1];
[button1 setNeedsDisplay];
animation1.animationDuration = 1;
animation1.animationRepeatCount = 3;
[animation1 startAnimating];
[super viewDidLoad];
}
但按钮内什么也没有出现。有什么想法我可能会出错吗?
另外,我希望根据用户按下按钮时显示的图像来调用不同的方法。我之前实现了这一点,但创建了一个图像数组,使用 NSTimer 设置 UIButton 的背景图像以循环浏览这些图像,然后将按下时按钮的图像数据与数组中的图像进行比较,调用不同的方法每个方法。这看起来确实不太有效!因此,为什么我尝试在子视图中循环图像。任何关于按下按钮时如何从 UIButton 的 UIImageView 子视图中提取图像数据的建议将不胜感激:)
谢谢!
迈克尔
I'm trying to create a button which displays animated images. I read in this post the suggestion that I add an UIImageView (with an array of animationImages) as a subview of the UIButton. I tried to do this like so:
(animation1 is a UIImageView object declared in the header)
- (void)viewDidLoad {
UIImage *image1 = [UIImage imageNamed:@"Good1.png"];
UIImage *image2 = [UIImage imageNamed:@"Good2.png"];
UIImage *image3 = [UIImage imageNamed:@"Bad1.png"];
NSArray *images = [[NSArray alloc] initWithObjects:image1, image2, image3, nil];
animation1.animationImages = images;
[images release];
[self.button1 addSubview:animation1];
[self.button1 bringSubviewToFront:animation1];
[button1 setNeedsDisplay];
animation1.animationDuration = 1;
animation1.animationRepeatCount = 3;
[animation1 startAnimating];
[super viewDidLoad];
}
But nothing appears within the button. Any ideas where I might be going wrong?
Also, I would like it so that different methods are called depending on which image is showing when the user presses the button. I achieved this previously but creating an array of images, using an NSTimer to set the background image of the UIButton to cycle to through these images, and then comparing the image data of the button when pressed to the images in the array, calling a different method for each. This really doesn't seem very efficient! Hence why I'm trying to cycle the images in a subview. Any suggestions of how I might extract imagedata from the UIImageView subview of the UIButton when the button is pressed would be most appreciated :)
Thanks!
Michael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这纯粹是猜测,但有时您需要显式设置框架。 (例如,当向 UIBarButtonItem 添加自定义按钮时)尝试添加一行
This is pure speculation, but sometimes you need to set a frame explicitly. (e.g. when adding a custom button to a UIBarButtonItem) Try adding a line