由於項目的需要,要設計能評分、要能顯示評分數據的星級評分條。但是ios上好像沒有這個控件,所以只有自己想辦法解決。對於這個功能需求,我總共想了兩個方法,
方法一:(這種方法比較愚)
思路:這種方法是直接在storyboard上的viewController裡面直接建5個UIImageView,初始值圖片設置為“button_star_white”。
第一步,在storyboard中的viewController上推拽5個人UIImageView,連接到 “ViewController. h” 文件中,作為viewController的成員變量代碼如下:
@property (weak, nonatomic) IBOutlet UIImageView *myImageview1;
@property (weak, nonatomic) IBOutlet UIImageView *myImageview2;
@property (weak, nonatomic) IBOutlet UIImageView *myImageview2;
@property (weak, nonatomic) IBOutlet UIImageView *myImageview3;
@property (weak, nonatomic) IBOutlet UIImageView *myImageview4;
@property (weak, nonatomic) IBOutlet UIImageView *myImageview5;
第二步,為每個imageView添加點擊事件,代碼如下:
_myImageview1.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress1)];
[_myImageview1 addGestureRecognizer:singleTap1];
_myImageview2.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonpress2)];
[_myImageview2 addGestureRecognizer:singleTap1];
...
(5個imageView同樣方式,再次不一一贅述)
第三步,實現方方法:buttonpress1,buttonpress2,…3,4,5,代碼如下:
- (void) buttonpress1{
_myImageview1.image = [UIImage imageNamed:@"button_star_red"];
_myImageview2.image = [UIImage imageNamed:@"button_star_white"];
_myImageview3.image = [UIImage imageNamed:@"button_star_white"];
_myImageview4.image = [UIImage imageNamed:@"button_star_white"];
_myImageview5.image = [UIImage imageNamed:@"button_star_white"];
}
- (void) buttonpress1{
_myImageview1.image = [UIImage imageNamed:@"button_star_red"];
_myImageview2.image = [UIImage imageNamed:@"button_star_red"];
_myImageview3.image = [UIImage imageNamed:@"button_star_white"];
_myImageview4.image = [UIImage imageNamed:@"button_star_white"];
_myImageview5.image = [UIImage imageNamed:@"button_star_white"];
}
. . .
(5個imageView同樣方式,再次不一一贅述)
好了,講過上述的幾個步驟,點擊星星打分,就可以實現了,不過,說過了,這個方法很愚,因為重復代碼的太多,而且還不支持滑動。下面我來介紹第二種方法。
方法二(這種方法相對高級一點,但是絕對不是最高級,我相信,優秀的馬努,人才輩出)
思路:ios 中 touchesBegan和touchesMoved兩個方法可以獲取到UIView上的點擊的坐標和滑動的坐標,根據坐標,位於X坐標左邊的imageview設置為“button_star_red”,右邊的設置為“button_star_red”。
第一步:在UIViewController上添加一個UIView(兩種方式,直接拖拽或者在”.m”文件代碼創建,這裡直接拖拽)命名為:myview,width 為星星寬度的6倍(總共5顆星星,確保可以0分),heigh為星星的高度
@property (weak, nonatomic) IBOutlet UIView *myview;
第二步:在myview上添加imageview,將星星初始化為“button_star_white”,並且按順序加入到數組中,便於後期遍歷改變星星顏色,代碼如下:
UIImageView *imageView;
for (int i = 0; i < 5; i++) {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"button_star_white"]];
imageView.frame = CGRectMake(_myview.bounds.origin.x+((i+1)*24), _myview.bounds.origin.y, 24, 24);
[_myview addSubview:imageView];
[_allStar addObject:imageView];
}
第三步:獲取點擊活著滑動的坐標,根據坐標,將坐標X以左的星星置為“button_star_white”(星星的寬和高都是24,)
#pragma mark - 點擊的坐標
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:_myview];
UIImageView *im ;
for(int i = 0;i < 5 ; i++){
im = _allStar[i];
NSLog(@"_all[%i] = (%f,%f)",i,im.frame.origin.x,im.frame.origin.y);
if ((touchPoint.x > 0)&&(touchPoint.x < 144)&&(touchPoint.y > 0)&&(touchPoint.y < 24)) {
NSString *myscore = [NSString stringWithFormat:@"分數為:%i",((int)touchPoint.x)/24];
_score.text = myscore;//_score是一個UILable,myscore為分數,顯示在給用戶看,關於這個不在贅述
if (im.frame.origin.x > touchPoint.x) {
im.image =[UIImage imageNamed:@"button_star_white"];
}else{
im.image =[UIImage imageNamed:@"button_star_red"];
}
}
}
}
#pragma mark - 滑動的坐標
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:_myview];
UIImageView *im ;
for(int i = 0;i < 5 ; i++){
im = _allStar[i];
NSLog(@"_all[%i] = (%f,%f)",i,im.frame.origin.x,im.frame.origin.y);
if ((touchPoint.x > 0)&&(touchPoint.x < 144)&&(touchPoint.y > 0)&&(touchPoint.y < 24)) {
NSString *myscore = [NSString stringWithFormat:@"分數為:%i",((int)touchPoint.x)/24];
_score.text = myscore;//_score是一個UILable,myscore為分數,顯示在給用戶看,關於這個不在贅述
if (im.frame.origin.x > touchPoint.x) {
im.image =[UIImage imageNamed:@"button_star_white"];
}else{
im.image =[UIImage imageNamed:@"button_star_red"];
}
}
}
}
怎麼樣,與第一種方法,第二種方法效果是不是更智能,過程更明了。細心的讀者會發現第二種方式的打分還可以支持小數