美文网首页程序员
仿商城评价星级

仿商城评价星级

作者: 答案在风中飘 | 来源:发表于2018-07-05 15:09 被阅读7次
    用户可以用手指划过控件上面的若干图像,以此来对电影、软件等项目做出评分。除了简单的滑动,还添加了动画效果。直接上代码,导入到自己项目里就可以直接用
    
    
    
    #import "StarSliderA.h"
    
    #define WIDTH [UIScreen mainScreen].bounds.size.width / 20
    
    @interfaceStarSliderA ()
    
    @property (nonatomic, assign) int value;
    
    @end
    
    .m文件
    @implementation StarSliderA
    
    - (instancetype)initWithFrame:(CGRect)aFrame{
    
    self = [superinitWithFrame:aFrame];
    
    if (self) {
    
    float offsetCenter = WIDTH;
    
    for (int i = 1; i <= 10; i++) {
    
    UIImageView *imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(0.0f, 0.0f, WIDTH, WIDTH)];
    
    imageView.image = [UIImageimageNamed:@"OFF_ART"];
    
    imageView.center = CGPointMake(offsetCenter, self.intrinsicContentSize.height / 2.0f);
    
    offsetCenter += WIDTH * 1.5f;
    
    [selfaddSubview:imageView];
    
    }
    
    }
    
    self.backgroundColor = [[UIColorblackColor] colorWithAlphaComponent:0.25f];
    
    returnself;
    
    }
    
    - (CGSize)intrinsicContentSize{
    
    returnCGSizeMake(WIDTH * 8.0f, 34.0f);
    
    }
    
    - (void)updateValueAtPoint : (CGPoint)point{
    
    int newValue = 0;
    
    UIImageView *changedView = nil;
    
    for (UIImageView *eachItem in [selfsubviews]) {
    
    if (point.x < eachItem.frame.origin.x) {
    
    eachItem.image = [UIImageimageNamed:@"OFF_ART"];
    
    }
    
    else{
    
    changedView = eachItem;
    
    eachItem.image = [UIImageimageNamed:@"ON_ART"];
    
    newValue++;
    
    }
    
    }
    
    if (self.value != newValue) {
    
    self.value = newValue;
    
    [selfsendActionsForControlEvents:UIControlEventValueChanged];
    
    [UIViewanimateWithDuration:0.15fanimations:^{
    
    changedView.transform = CGAffineTransformMakeScale(1.5f, 1.5f);
    
    } completion:^(BOOL done) {
    
    [UIViewanimateWithDuration:0.1fanimations:^{
    
    changedView.transform = CGAffineTransformIdentity;
    
    }];
    
    }];
    
    }
    
    }
    
    - (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    
    CGPoint touchPoint = [touch locationInView:self];
    
    [selfsendActionsForControlEvents:UIControlEventTouchDown];
    
    [selfupdateValueAtPoint:touchPoint];
    
    returnYES;
    
    }
    
    - (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    
    CGPoint touchPoint = [touch locationInView:self];
    
    if (CGRectContainsPoint(self.frame, touchPoint)) {
    
    [selfsendActionsForControlEvents:UIControlEventTouchUpInside];
    
    }
    
    else
    
    [selfsendActionsForControlEvents:UIControlEventTouchUpOutside];
    
    [selfupdateValueAtPoint:[touch locationInView:self]];
    
    returnYES;
    
    }
    
    - (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    
    CGPoint touchPoint = [touch locationInView:self];
    
    if (CGRectContainsPoint(self.bounds, touchPoint)) {
    
    [selfsendActionsForControlEvents:UIControlEventTouchUpInside];
    
    }
    
    else
    
    [selfsendActionsForControlEvents:UIControlEventTouchUpOutside];
    
    }
    
    - (void)cancelTrackingWithEvent:(UIEvent *)event{
    
    [selfsendActionsForControlEvents:UIControlEventTouchCancel];
    
    }
    
    @end
    
    

    相关文章

      网友评论

        本文标题:仿商城评价星级

        本文链接:https://www.haomeiwen.com/subject/bespuftx.html