OC实现通过星星进行评分

1.思路:在视图上先放一排灰色的星星,再放置同等大小与尺寸的亮色星星,通过改变上面亮色星星的父视图的宽度来进行改变;
2.创建视图代码:

#define STAR_IMAGE @"icon_wjx2"//暗色星星图片
#define SELECTED_STAR_IMAGE @"icon_wjx1"//亮色星星图片
typedef NS_ENUM(NSInteger,RateStyle){
    WholeStar,//整星
    IncompleteStar//不整星
};
@class StarRateView;
@protocol StarRateViewDelegate <NSObject>
@optional
- (void)starRateView:(StarRateView *)view didFinishRate:(CGFloat )rate;
@end
@interface StarRateView : UIView
@property (nonatomic, weak)id<StarRateViewDelegate>delegate;
/**选择评分的模式*/
@property (nonatomic, assign)RateStyle rateStyle;
/**当前选中的评分*/
@property (nonatomic, assign)CGFloat currentRate;
/**初始化方法*/
- (instancetype)initWithFrame:(CGRect)frame AndStarCount:(NSInteger )starCount;

@end

#import "StarRateView.h"
@interface StarRateView ()
@property (nonatomic, assign)NSInteger numberOfStars;
@property (nonatomic, strong)UIView *foregroundStarView;
@property (nonatomic, strong)UIView *backgroundStarView;
@end
@implementation StarRateView
- (instancetype)initWithFrame:(CGRect)frame AndStarCount:(NSInteger)starCount {
    if (self = [super initWithFrame:frame]) {
        self.numberOfStars = starCount;
        self.currentRate = 0;
        self.userInteractionEnabled = YES;
        [self setUpStarView];
    }
    return self;
}
#pragma mark - 设置界面
- (void)setUpStarView {
    self.foregroundStarView = [self createStarViewWithImage:SELECTED_STAR_IMAGE];
    
    self.backgroundStarView = [self createStarViewWithImage:STAR_IMAGE];
    
    self.foregroundStarView.frame = CGRectMake(0, 0, self.currentRate * (self.bounds.size.width / self.numberOfStars), self.bounds.size.height);
    
    [self addSubview:self.backgroundStarView];
    
    [self addSubview:self.foregroundStarView];
    
    //添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapRateView:)];
    tap.numberOfTapsRequired = 1;
    [self addGestureRecognizer:tap];
    
}
#pragma mark 创建星星视图
- (UIView *)createStarViewWithImage:(NSString *)imageName{
    UIView *view = [[UIView alloc]initWithFrame:self.bounds];
    view.clipsToBounds = YES;
    view.backgroundColor = [UIColor clearColor];
    
    for (int i = 0; i < self.numberOfStars; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
        imageView.frame = CGRectMake(i * self.bounds.size.width / self.numberOfStars, 0, self.bounds.size.width / self.numberOfStars, self.bounds.size.height);
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        [view addSubview:imageView];
    }
    return view;
}
#pragma mark - 点击了星星
-(void)tapRateView:(UITapGestureRecognizer *)tap {
    CGPoint tapPoint = [tap locationInView:self];
    
    CGFloat offsset = tapPoint.x;
    
    CGFloat rate = offsset / (self.bounds.size.width / self.numberOfStars);
    
    switch (self.rateStyle) {
        case WholeStar:
        {
            self.currentRate = ceilf(rate);
        }
            break;
        case IncompleteStar:
        {
            self.currentRate = rate;
        }
        default:
            break;
    }
}
#pragma mark -点击星星后重绘界面
- (void)layoutSubviews {
    [super layoutSubviews];
    [UIView animateWithDuration:0.2 animations:^{
        self.foregroundStarView.frame = CGRectMake(0, 0, self.currentRate * (self.bounds.size.width / self.numberOfStars), self.bounds.size.height);
    }];
}
#pragma mark -设置当前评分
- (void)setCurrentRate:(CGFloat)currentRate {
    if (_currentRate == currentRate) {
        return;
    }
    if (currentRate > self.numberOfStars) {
        NSLog(@"评分不能超过星星数量")
    }
    if (currentRate < 0) {
        NSLog(@"评分不能为负数");
    }
    if (self.rateStyle == WholeStar) {
        currentRate = ceilf(currentRate);
    }
    _currentRate = currentRate;
    [self setNeedsLayout];
    if ([self.delegate respondsToSelector:@selector(starRateView:didFinishRate:)]) {
        [self.delegate starRateView:self didFinishRate:self.currentRate];
    }
}

3.使用方法:

view.rateStyle = IncompleteStar;
    view.delegate = self;
    view.currentRate = 2.1;
    [self.view addSubview:view];
#pragma mark - StarRateViewDelegate
- (void)starRateView:(StarRateView *)view didFinishRate:(CGFloat)rate {
    NSLog(@"%.2f",rate);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
红绿色盲的主要特征是对红色和绿色颜色的区分能力降低。因此,我们可以通过CIColorMatrix滤镜将图像的红色和绿色通道进行转换,从而实现对红绿色盲的过滤。下面是OC代码实现: ``` //获取原始图像 CIImage *inputImage = [CIImage imageWithCGImage:image.CGImage]; //创建颜色矩阵 const CGFloat colorMatrix[] = { 0.567, 0.433, 0, 0, 0, 0.558, 0.442, 0, 0, 0, 0, 0.242, 0.758, 0, 0, 0, 0, 0, 1, 0 }; CIVector *colorMatrixVector = [CIVector vectorWithValues:colorMatrix count:16]; //创建CIColorMatrix滤镜 CIFilter *colorMatrixFilter = [CIFilter filterWithName:@"CIColorMatrix"]; [colorMatrixFilter setValue:inputImage forKey:kCIInputImageKey]; [colorMatrixFilter setValue:colorMatrixVector forKey:@"inputRVector"]; [colorMatrixFilter setValue:colorMatrixVector forKey:@"inputGVector"]; [colorMatrixFilter setValue:colorMatrixVector forKey:@"inputBVector"]; [colorMatrixFilter setValue:colorMatrixVector forKey:@"inputAVector"]; //获取输出图像 CIImage *outputImage = [colorMatrixFilter outputImage]; //将CIImage转换成UIImage CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef imageRef = [context createCGImage:outputImage fromRect:outputImage.extent]; UIImage *resultImage = [UIImage imageWithCGImage:imageRef]; //释放CGImageRef CGImageRelease(imageRef); //返回结果 return resultImage; ``` 以上代码实现了对原始图像进行红绿色盲过滤的效果。在颜色矩阵中,我们将红色通道的权值设置为0.567,将绿色通道的权值设置为0.442,从而实现了对红绿色的转换。你可以根据自己的需求,修改颜色矩阵,实现不同的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值