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