日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php 五颗星评价,简单实现点触/输入值给五颗星评价

發(fā)布時(shí)間:2025/3/11 php 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 五颗星评价,简单实现点触/输入值给五颗星评价 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

先上效果圖

gif.gif

1.碼UI。。。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];

label.text = @"點(diǎn)擊星星可以自動(dòng)獲取評(píng)分哦~";

label.textColor = [UIColor whiteColor];

label.textAlignment = NSTextAlignmentCenter;

[self.view addSubview:label];

label.center = CGPointMake(kScreenWidth / 2, 60);

self.colorStars = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"StarsForeground"]];

self.blankStars = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"StarsBackground"]];

// 寬高根據(jù)Image實(shí)際大小,如果放到了2倍圖記得?2

self.colorStars.frame = CGRectMake((kScreenWidth - 65) / 2, 100, 65, 23);

self.blankStars.frame = CGRectMake((kScreenWidth - 65) / 2, 100, 65, 23);

// 順序不能錯(cuò)

[self.view addSubview:self.blankStars];

[self.view addSubview:self.colorStars];

// 關(guān)鍵

self.colorStars.clipsToBounds = YES;

self.colorStars.contentMode = UIViewContentModeLeft | UIViewContentModeTop;

self.tf = [[UITextField alloc] initWithFrame:CGRectMake((kScreenWidth - 300) / 2, 250, 300, 30)];

self.tf.borderStyle = UITextBorderStyleRoundedRect;

self.tf.backgroundColor = [UIColor lightGrayColor];

self.tf.textColor = [UIColor blueColor];

self.tf.backgroundColor = [UIColor whiteColor];

self.tf.placeholder = @"請(qǐng)輸入1.0-5.0的評(píng)分等級(jí)";

self.tf.keyboardType = UIKeyboardTypeDecimalPad;

self.tf.textAlignment = NSTextAlignmentCenter;

[self.view addSubview:self.tf];

UIButton *Stars = [UIButton buttonWithType:UIButtonTypeCustom];

[Stars setTitle:@"評(píng)星" forState:UIControlStateNormal];

[Stars setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

Stars.frame = CGRectMake((kScreenWidth - 50) / 2, 350, 50, 30);

[Stars addTarget:self action:@selector(starsBtnClick) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:Stars];

2.評(píng)星按鈕的回調(diào)事件

- (void)starsBtnClick {

if ([self.tf.text floatValue] <= 0) {

NSLog(@"請(qǐng)輸入評(píng)分");

[self alertMSG:@"請(qǐng)輸入評(píng)分"];

} else if ([self.tf.text floatValue] > 5) {

NSLog(@"請(qǐng)按規(guī)則輸入評(píng)分");

[self alertMSG:@"請(qǐng)按規(guī)則輸入評(píng)分"];

} else {

CGFloat value = self.tf.text.floatValue;

CGFloat width = value/5.0*65;

CGPoint orgin = self.colorStars.frame.origin;

CGFloat height = self.colorStars.frame.size.height;

[UIView animateWithDuration:0.23 animations:^{

self.colorStars.frame = CGRectMake(orgin.x, orgin.y, width, height);

}];

}

}

3.點(diǎn)觸評(píng)星的實(shí)現(xiàn)

// 點(diǎn)觸評(píng)分的關(guān)鍵

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

[super touchesEnded:touches withEvent:event];

UITouch *touche = [touches anyObject];

CGPoint touchPoint = [touche locationInView:self.blankStars];

NSLog(@"%@", NSStringFromCGPoint(touchPoint));

// 如果點(diǎn)觸在圖片外,就不進(jìn)行操作

if (touchPoint.x >= 0 && touchPoint.y >= 0 && touchPoint.x < self.blankStars.frame.size.width + 10 && touchPoint.y <= self.blankStars.frame.size.height) {

CGPoint orgin = self.colorStars.frame.origin;

CGFloat height = self.colorStars.frame.size.height;

CGFloat touchX = touchPoint.x;

if (touchPoint.x > self.blankStars.frame.size.width) {

touchX = self.blankStars.frame.size.width;

}

[UIView animateWithDuration:0.23 animations:^{

self.colorStars.frame = CGRectMake(orgin.x, orgin.y, touchX, height);

} completion:^(BOOL finished) {

self.tf.text = [NSString stringWithFormat:@"%.2lf", touchX / self.blankStars.frame.size.width * 5];

}];

}

[self.view endEditing:YES];

}

總結(jié)

以上是生活随笔為你收集整理的php 五颗星评价,简单实现点触/输入值给五颗星评价的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。