【问题标题】:Subclassing UIActivityIndicator to change the Image子类化 UIActivityIndi​​cator 以更改图像
【发布时间】:2012-12-05 06:23:47
【问题描述】:

这就是我所做的:

@implementation BGUIActivityIndicator

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    [self customInitialize];
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    [self customInitialize];
    return self;
}

-(void)customInitialize
{
    UIView * theImageView= [self findASubViewforClass:[UIImageView class]];//
    UIImageView * theImageView1 = (UIImageView *) theImageView;
    theImageView1.image = [UIImage imageNamed:@"spinner_blue"];
    [theImageView1.image saveScreenshot];
    while (false) ;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

一切看起来都很完美。 [theImageView1.image saveScreenshot];完美记下新旧观点。

但是,没有任何变化。为什么?

我并不是在问如何更改 UIActivityIndi​​cator 的图像。已经有很多了。我想通过继承 UIActivityIndi​​cator 来使用它,因为我认为它是最优雅的解决方案。我好像做不到。

特别是,我在问为什么我的方法(例如,用于更改搜索控制器的背景)不适用于此。

【问题讨论】:

    标签: objective-c xcode4.5


    【解决方案1】:

    根据UIActivityIndicatorView Class Reference,没有办法/机会通过子分类来改变图像。

    但是您可以更改其 activityIndi​​catorViewStyle 、活动指示器的颜色、UIActivityIndi​​catorStyle 等。

    我认为,在没有子类化的情况下,类 UIImageView 提供了一种非常有用且简单的方法来实现这样的事情。您唯一需要做的就是:

    1.Provide a number of images that reflect your indicator animation.
    2.Create a new UIImageView instance and set images and animation duration.
    3.Position your custom activity indicator within your current view.
    

    示例代码:

    //Create the first status image and the indicator view
    UIImage *statusImage = [UIImage imageNamed:@"status1.png"];
    UIImageView *activityImageView = [[UIImageView alloc] 
                    initWithImage:statusImage];
    
    
    //Add more images which will be used for the animation
    activityImageView.animationImages = [NSArray arrayWithObjects:
                [UIImage imageNamed:@"status1.png"],
                [UIImage imageNamed:@"status2.png"],
                [UIImage imageNamed:@"status3.png"],
                [UIImage imageNamed:@"status4.png"],
                [UIImage imageNamed:@"status5.png"],
                [UIImage imageNamed:@"status6.png"],
                [UIImage imageNamed:@"status7.png"],
                [UIImage imageNamed:@"status8.png"],
                nil];
    
    
    //Set the duration of the animation (play with it
    //until it looks nice for you)
    activityImageView.animationDuration = 0.8;
    
    
    //Position the activity image view somewhere in 
    //the middle of your current view
    activityImageView.frame = CGRectMake(
                self.view.frame.size.width/2
                    -statusImage.size.width/2, 
                self.view.frame.size.height/2
                    -statusImage.size.height/2, 
                statusImage.size.width, 
                statusImage.size.height);
    
    //Start the animation
    [activityImageView startAnimating];
    
    
    //Add your custom activity indicator to your current view
    [self.view addSubview:activityImageView];
    

    查看完整详情Here

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多