使用百分比揭示UIImageView

时间:2016-09-01 09:18:48

标签: ios objective-c uiimageview uiimage

我正在尝试创建各种条形图,我计划使用两个图像,一个颜色的UIImage覆盖,但暗灰色的UIImage。随着用户的进步,我想将暗灰色UIImageView的大小缩小到右边x%。

2 个答案:

答案 0 :(得分:1)

您可以将dullImageView与3侧(顶部,底部和左/右(不是两个!))的colouredImageView完全对齐, 然后为dullImageView设置宽度约束(如果条形图是垂直的,则设置高度)。

将约束出口引用到您的代码中(右键单击 - 将约束拖动到您的类文件中)并相应地更改约束的常量值,下面的示例将减少30%。

total

答案 1 :(得分:0)

鉴于您对上述答案的评论(如果您没有尝试在tableView中执行此操作的多个实例并且确实按照要求解决您的问题,那么这会很有效,所以我给它+1了)我建议您自己UITableViewCell上的子类。有时候你只需要亲自动手并编写一些代码。最佳:)

//  BarChartTableViewCell.h
//  Created by Jef Long on 2/09/2016.
#import <UIKit/UIKit.h>

@interface BarChartTableViewCell : UITableViewCell

@property (readonly) UIImageView *underImageView , *overImageView ;
@property CGFloat coverPercent ; //eg 0.36 = 36%

@end

@implementation

//  BarChartTableViewCell.m
//  Created by Jef Long on 2/09/2016.

#import "BarChartTableViewCell.h"

@implementation BarChartTableViewCell
{
//ivars
 UIImageView *_underImageView , *_overImageView ;

}

- (void)awakeFromNib {
    [super awakeFromNib] ;

    [self setUp] ;
    // Initialization code
}

-(void)setUp{
    //this may need to be called from some initialiser or other depending how you do your tableView
    self.contentView.translatesAutoresizingMaskIntoConstraints = YES ;
    if ( _underImageView == nil ){
      _underImageView = [[UIImageView alloc]initWithFrame: self.contentView.bounds ] ;
      _underImageView.autoResizingMask = UIViewAutoresizingFlexibleWidth |
      UIViewAutoresizingFlexibleHeight ;
      [self.contentView addSubview: _underImageView ] ;
    }
    if ( _overImageView == nil ){
    _overImageView = [[UIImageView alloc]initWithFrame: self.contentView.bounds ] ;
    _overImageView.autoResizingMask = UIViewAutoresizingFlexibleWidth |
    UIViewAutoresizingFlexibleHeight |  UIViewAutoresizingFlexibleLeftMargin ;
    [self.contentView addSubview: _overImageView ] ;
    }

}
-(void)setCoverPercent:(CGFloat ) coverPercent{
    [UIView animateWithDuration:0.3
                     animations:{

                         _overImageView.frame = CGRectMake( self.contentView.bounds.size.width - (self.contentView.bounds.size.width*coverPercent) , 0.0 , self.contentView.bounds.size.height , self.contentView.bounds.size.width*coverPercent  ) ;

                     }
                     completion:(BOOL finished){} ]

}
-(CGFloat )coverPercent{
    return (_overImageView.bounds.size.with / _underImageView.bounds.size.with ) ;
}



-(UIImageView *) underImageView{
    return _underImageView ;
}

-(UIImageView *) overImageView{
    return _overImageView ;
}

@end