五星好评及刮刮乐

项目中我们可能会遇到,在订单完成后评价的界面,以及刮奖的界面,我简单的说一下我的实现方法,效果如下:

五星好评及刮刮乐_第1张图片
五星好评以及刮奖.gif

首先先说说五星好评界面:
1、先说一下思路

五星好评及刮刮乐_第2张图片
95838E80-57A8-453A-8AEB-85332D1F620F.png

这其实就是五张图片,通过监听手指移动的位置,来进行图片的变化
主要就是三个方法
- (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event;//手指开始点击
- (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event;//手指开始移动
- (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event;//手指离开
2、实现

//手指开始点击,点击的位置判断是否添加星星
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches  anyObject];
CGPoint point = [touch locationInView:self];
//判断点击的位置是否在视图上
if (point.x >= self.firstStarImage.x && point.x <= (self.width) && (point.y > self.firstStarImage.y & point.y< self.height )) {
//使用变量来判断是否添加星星
    self.isAddStar = YES;
   }else {
    self.isAddStar = NO;
   }
}
//手指开始移动,在移动到的位置添加星星
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(self.isAddStar){
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    //添加星星
    [self setStarForegroundViewWithPoint:point];
    }
}
//添加星星计算分数
- (void)setStarForegroundViewWithPoint:(CGPoint)point {
//添加星星
float score = 0;
score += [self changeImg:point.x image:self.firstStarImage];
score += [self changeImg:point.x image:self.secondStarImage];
score += [self changeImg:point.x image:self.threeStarImage];
score += [self changeImg:point.x image:self.fourStarImage];
score += [self changeImg:point.x image:self.fiveStarImage];
//最少得半个星星
if (score == 0) {
    score = 0.5;
    if (!self.halfImageStr) {
        self.halfImageStr = @"StarSelectHalf";
    }
    [self.firstStarImage setImage:[UIImage imageNamed:self.halfImageStr]];
 }
self.score = score;
}
//设置添加星星,返回分数
-(CGFloat)changeImg:(float)x image:(UIImageView*)imgView{
if (!self.normalStr) {
    self.normalStr = @"StarNormal";
}
if (!self.selectStr) {
    self.selectStr = @"StarSelected";
}
if (!self.halfImageStr) {
    self.halfImageStr = @"StarSelectHalf";
}

if(x > (imgView.x + imgView.width/2)){
    imgView.image =  [UIImage imageNamed:self.selectStr];
    return 1;
}else if(x > imgView.x){
    imgView.image =  [UIImage imageNamed:self.halfImageStr];
    return 0.5;
}else{
    imgView.image =  [UIImage imageNamed: self.normalStr];
    return 0;
}
}
//结束的时候,将是否添加星星变为NO;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.isAddStar = NO;
}

3、主要代码就这些了,整个我也做了封装,如果需要的话可以去下面的链接地址里下载。

其次我们说一说刮奖的实现。
1、先说思路


五星好评及刮刮乐_第3张图片
EAA2ED39-91A1-4764-B5DB-75C38AE644AC.png

主要就是两张图片和一个label,刮开遮盖图片就可以看到label上的文字了。
2、实现
也是使用的和好评一样的方法。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//得到移动时候的点
UITouch *touch = [touches anyObject];
CGPoint contentPoint = [touch locationInView:coverImageView];
//清除的大小
CGRect rect = CGRectZero;
if (pointSize.width) {
   rect = CGRectMake(contentPoint.x, contentPoint.y, pointSize.width, pointSize.height);
}else{
   rect = CGRectMake(contentPoint.x, contentPoint.y, 10, 10);
}
//得到图片的一个视图
UIGraphicsBeginImageContextWithOptions(coverImageView.bounds.size, NO, 0);
//获取上下文
CGContextRef ref = UIGraphicsGetCurrentContext();
//将图片映射到上下文中
[coverImageView.layer renderInContext:ref];
//清除划过的区域
CGContextClearRect(ref, rect);
//获取图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
coverImageView.image = image;
}
//点击结束,判断文字的四个角是否都完全显示出来,显示出来就回调,然后执行刮开后的代码
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint leftTopPoint =  [self convertPoint:_textLabel.frame.origin toView:coverImageView];
CGPoint leftBottomPoint = [self convertPoint:CGPointMake(_textLabel.frame.origin.x, _textLabel.y + _textLabel.height * 0.5) toView:coverImageView];
CGPoint rightTopPoint =  [self convertPoint:CGPointMake(_textLabel.x +_textLabel.width * 0.5, _textLabel.y) toView:coverImageView];
CGPoint rightBottomPoint = [self convertPoint:CGPointMake(_textLabel.x +_textLabel.width * 0.5, _textLabel.y + _textLabel.height * 0.5) toView:coverImageView];
if ([self isOpaqueWithPoint:leftTopPoint]&&[self isOpaqueWithPoint:leftBottomPoint]&&[self isOpaqueWithPoint:rightBottomPoint]&&[self isOpaqueWithPoint:rightTopPoint]) {
    if ([self.delegate respondsToSelector:@selector(scratchViewTextShow:)]) {
        [self.delegate scratchViewTextShow:self];
    }
  }
}
//判断某个点是否透明
-(BOOL)isOpaqueWithPoint:(CGPoint)point{
unsigned char pixel[1]={0};
CGContextRef context = CGBitmapContextCreate(pixel,1,1,8,1,NULL,kCGImageAlphaOnly);
UIGraphicsPushContext(context);
[coverImageView.image drawAtPoint: CGPointMake(-point.x,-point.y)];
UIGraphicsPopContext();
CGContextRelease(context);
CGFloat alpha = pixel[0]/255.0f;
BOOL transparent = alpha<0.01f;
return transparent;
}

大概就这些步骤了,如有需要可以下载:https://github.com/zhangyqyx/EvaluationWithTombola
希望大家能提出宝贵的意见,可以给我留言,也可以发邮件到我的邮箱:[email protected]
谢谢大家,如果你有更好的想法或文章请告知,不胜感激。

你可能感兴趣的:(五星好评及刮刮乐)