如何使用此方法在占位符中执行活动指示器?
- (void)createActivityIndicatorWithStyle:(UIActivityIndicatorViewStyle) activityStyle {
if ([self activityIndicator] == nil) {
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:activityStyle];
self.activityIndicator.autoresizingMask = UIViewAutoresizingNone;
//calculate the correct position
float width = self.activityIndicator.frame.size.width;
float height = self.activityIndicator.frame.size.height;
float x = (self.frame.size.width / 2.0) - width/2;
float y = (self.frame.size.height / 2.0) - height/2;
self.activityIndicator.frame = CGRectMake(x, y, width, height);
self.activityIndicator.hidesWhenStopped = YES;
[self addSubview:self.activityIndicator];
}
[self.activityIndicator startAnimating];}
感谢您的帮助
发布于 2014-03-07 17:44:46
您可以这样做,我在本例中使用它来更新单元格,但它对于您需要的任何用途都是相同的。
我正在做的是将UIImageView alpha设置为0,添加一个UIActivityIndicatore,并调用一个块以在图像加载了bees时得到通知。加载时,UIImageView alpha将被设置回1,并删除UIActivityIndicator。
-(void)updateCell {
self.imageView = [[UIImageView alloc]initWithFrame:frameOfCell];
self.imageView.alpha = 0;
[self addSubview:self.imageView];
//Activity indivator:
self.activity= [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activity.hidesWhenStopped = YES;
self.activity.frame = CGRectMake((frameOfCell.size.width/2)-10, (frameOfCell.size.height/2)-20, 40, 40);
[self.activity startAnimating];
[self addSubview:self.activity];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
NSURL *urlOfImage = [NSURL URLWithString:self.imageName];
self.imageView.alpha = 0;
[self.imageView setImageWithURL:urlOfImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
///
self.imageView.alpha = 1;
//Removing activity indicator:
[self.activity stopAnimating];
[self.activity removeFromSuperview];
}];
}希望这能有所帮助
发布于 2014-03-07 17:48:27
这是a Category on UIImageView, adding a progress view while images are downloaded using SDWebImage.
您应该做的是使用UIActivityIndicatorView更改UIProgressView
试着像这样修改代码:
- (void)removeIndicatorView{
[self.activityIndicator stopAnimating];
}
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options completed:(SDWebImageCompletedBlock)completedBlock withActivityStyle:(UIActivityIndicatorViewStyle) activityStyle {
[self createActivityIndicatorWithStyle:activityStyle];
__weak typeof(self) weakSelf = self;
[self setImageWithURL:url
placeholderImage:placeholder
options:options
progress:^(NSUInteger receivedSize, long long expectedSize) {
//If you don't care progress, do nothing
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
[weakSelf removeIndicatorView];
if (completedBlock) {
completedBlock(image, error, cacheType);
}
}
];
}https://stackoverflow.com/questions/22245480
复制相似问题