我正在尝试做一个UIButton
的简单2帧动画。我了解这可以使用UIButton's
imageView.animationImages
属性 - 将其设置为数组并为其设置动画。但是当我这样做时,什么都没有出现。仅供参考,我已经检查了buttonImages数组,它确实包含了我想要显示的图像。我出错的任何想法?
NSArray *buttonImages = bookDoc.book.buttonImages;
UIImage *bookImage = bookDoc.thumbImage;
UIButton *bookButton = [UIButton buttonWithType:UIButtonTypeCustom];
bookButton.frame = CGRectMake(0, 0, bookImage.size.width, bookImage.size.height);
//[bookButton setBackgroundImage:bookImage forState:UIControlStateNormal];
bookButton.imageView.animationImages = buttonImages;
bookButton.imageView.animationDuration = 0.5;
bookButton.imageView.animationRepeatCount = INFINITY;
[bookButton addTarget:self action:@selector(bookButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[bookButton.imageView startAnimating];
答案 0 :(得分:3)
imageView
的{{1}}初始化为(0,0,0,0)的帧,即它不可见(并且它实际上也是隐藏的)。
您可以自行配置UIButton
和frame
属性:
hidden
或者,更简单的是只使用动画的第一张图片
bookButton.imageView.frame = CGRectMake(0, 0, 20, 20);
bookButton.imageView.hidden = NO;
这将取消隐藏imageView并设置其正确的帧。
一旦imageView可见,它就会生成动画。
答案 1 :(得分:1)
我认为我从未尝试将图像保存到按钮的imageView属性中。我总是使用setImage:forState或setBackgroundImage:forState。
我的猜测是按钮正在设置图像视图本身的图像属性,使用(零)图像作为默认图像状态。
您是否尝试将button.imageView.image属性设置为静态图像以查看是否有效?我的猜测,就像我说的那样,按钮会覆盖你提供的任何图像,使用setImage:forState中提供的设置(如果你从未提供当前状态的图像,则为nil。)
答案 2 :(得分:0)
我刚写了这个,它有效。它的缺点是我在IB中设置的突出显示的图像似乎不起作用。 Xcode 5.也许禁用的图像等也不起作用。如果你不关心这个,那就行了。
+(void)animatemjad:(UIButton*)button offimagenames:(NSArray*)imagenames startdelay:(const NSTimeInterval)STARTDELAY {
UIImageView* imageview = [button imageView];
NSMutableArray* images = [NSMutableArray array];
for (NSString* imagename in imagenames) {
UIImage* image = [UIImage imageNamed:imagename];
NSAssert(image != nil, @"Failed loading imagename: %@", imagename);
if (image == nil)
return; // return out here... whatever
[images addObject:image];
}
imageview = [[UIImageView alloc] initWithImage:images.firstObject];
[button addSubview:imageview];
imageview.animationImages = images;
imageview.animationDuration = 5;
imageview.animationRepeatCount = 0;
[imageview performSelector:@selector(startAnimating) withObject:nil afterDelay:STARTDELAY]; // [imageview startAnimating];
}