简体   繁体   English

使用百分比发现UIImageView

[英]Uncover UIImageView using a percentage

I am trying to create a bar graph of sorts, I plan on using two image one coloured UIImage covered but a dull grey UIImage. 我正在尝试创建各种条形图,我计划使用两个图像,其中一个颜色覆盖UIImage,而一个暗灰色的UIImage。 As the user progresses I would like to reduce the size of the dull grey UIImageView to the right by x%. 随着用户的进步,我想将暗灰色UIImageView的大小向右减小x%。

You can align the dullImageView to the colouredImageView on 3 sides(top,bottom and left/right(not both!)) exactly on top, then set a width constraint for the dullImageView(or height if your bar chart is vertical). 您可以将dullImageView与colouredImageView的3个侧面(顶部,底部和左侧/右侧(不是两个!))恰好在顶部对齐,然后为dullImageView设置宽度约束(如果条形图是垂直的,则设置高度)。

Reference the constraint outlet into your code (rightclick-drag the constraint into your class file) and change the constant value of the constraint accordingly, an example below reduces by 30%. 将约束出口引用到您的代码中(右键单击约束并将其拖动到类文件中),并相应地更改约束的常量值,以下示例减少了30%。

float xPercent = 30; //for example you want a 30% decrease
[self.dullViewHeight setConstant: self.colouredView.frame.size.height*(1-(xPercent/100))];

In view of your comment on the answer above (which would work great if you were not trying to do multiple instances of this in a tableView and does address your question as asked so I gave it a +1 ) I suggest your own subclass on UITableViewCell . 考虑到您对以上答案的评论(如果您不尝试在tableView中执行多个此实例并且确实按要求解决了您的问题,那将非常有用,因此我给了+1),建议在UITableViewCell上使用您自己的子类。 Sometimes you just have to get your hands dirty and write a little code. 有时,您只需要动手并编写一些代码即可。 Best :) 最好 :)

//  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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM