自定义类似于淘宝的评价星星功能



//

//  StarView.h

//  Tiaozhu

//

//  Created by 杨继雷 on 15/8/20.

//  Copyright (c) 2015 Tiaozhuwang.com. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface StarView :UIView

{

    //背景图

    UIImageView *backgroundImageView;

    //前景图

    UIImageView *foregroundImageView;

}

//设置星级

-(void)setStar:(CGFloat)star;

-(void)addTapGesture;

-(void)addPanGesture;


@end

//

//  StarView.m

//  Tiaozhu

//

//  Created by 杨继雷 on 15/8/20.

//  Copyright (c) 2015 Tiaozhuwang.com. All rights reserved.

//


#import "StarView.h"


@implementation StarView

-(void)createImage

{

    backgroundImageView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"StarsBackground"]];

    backgroundImageView.frame=CGRectMake(0,0,66.5,10);

    backgroundImageView.contentMode=UIViewContentModeLeft;

    backgroundImageView.userInteractionEnabled =YES;

    foregroundImageView.userInteractionEnabled =YES;

    foregroundImageView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"StarsForeground"]];

    foregroundImageView.frame=CGRectMake(0,0,66.5,10);

    //66.5为五星的图片的宽度大小

    //设置内容的对齐方式

    foregroundImageView.contentMode=UIViewContentModeLeft;

    //如果子视图超出父视图大小时被裁剪掉

    foregroundImageView.clipsToBounds=YES;

    [self addSubview:backgroundImageView];

    [self addSubview:foregroundImageView]; 

   self.backgroundColor=[UIColor clearColor];

    

}

-(void)addTapGesture{

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTapRateView:)];

    tapGesture.numberOfTapsRequired = 1;

    [self addGestureRecognizer:tapGesture];

}

-(void)addPanGesture{

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];

    [backgroundImageView addGestureRecognizer:panGesture];

}


-(void)panGesture:(UIPanGestureRecognizer *)gesture{

   CGPoint tapPoint = [gesturelocationInView:backgroundImageView];

   CGFloat offset = tapPoint.x;

   NSLog(@"%f",offset/66.5);

    [selfsetStar:offset/66.5*5];

    

   if (offset>0) {

        [selfsetStar:offset/66.5*5];

        

    }else{

    }

    

}

- (void)userTapRateView:(UITapGestureRecognizer *)gesture {

   CGPoint tapPoint = [gesturelocationInView:backgroundImageView];

   CGFloat offset = tapPoint.x;

   NSLog(@"%f",offset/66.5);

    [selfsetStar:offset/66.5*5];

}

-(void)setStar:(CGFloat)star

{

    CGRect frame=backgroundImageView.frame;

    NSLog(@"%@",NSStringFromCGRect(frame));

    frame.size.width=frame.size.width*(star/5);

    foregroundImageView.frame=frame;

    

}

- (id)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self) {

        // Initialization code

        [selfcreateImage];

    }

    return self;

}


@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值