// 事件传递给subview - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { CGPoint subviewP = [self convertPoint:point toView:_subview]; if ([_subview pointInside:subviewP withEvent:event]) { return _subview; }else { return [super hitTest:point withEvent:event]; } } // 手指移动 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; CGPoint pre = [touch previousLocationInView:self]; CGFloat offsetX = location.x - pre.x; CGFloat offsetY = location.y - pre.y; CGPoint center = self.center; center.x += offsetX; center.y += offsetY; self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY); }
// // TeButton.m // tesg // // Created by apollo on 15/5/21. // Copyright (c) 2015年 apollo. All rights reserved. // #import "EvaluationButton.h" @interface EvaluationButton() { UILabel *_label; } @end @implementation EvaluationButton - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { CGPoint subviewP = [self convertPoint:point toView:_label]; if ([_label pointInside:subviewP withEvent:event]) { return self; }else { return [super hitTest:point withEvent:event]; } } - (void)layoutSubviews { [super layoutSubviews]; for (UIView *view in self.subviews) { if ([view isKindOfClass:[UILabel class]]) { _label = (UILabel *)view; view.frame = CGRectMake(view.frame.origin.x + 5, 0, view.frame.size.width + 15, self.frame.size.height - 10); view.backgroundColor = [UIColor clearColor]; if (self.isSelected) { [self setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected]; if ([self.currentTitle isEqualToString:@"好评"]) { view.backgroundColor = [self colorWithHexString:@"F4C745"]; } else if ([self.currentTitle isEqualToString:@"中评"]) { view.backgroundColor = [self colorWithHexString:@"#77CDFE"]; } else if ([self.currentTitle isEqualToString:@"差评"]) { view.backgroundColor = [self colorWithHexString:@"FB5551"]; } } UILabel *label = (UILabel *)view; label.textAlignment = NSTextAlignmentCenter; for (UIView *view2 in self.subviews) { if ([view2 isKindOfClass:[UIImageView class]]) { UIImageView *image = (UIImageView *)view2; label.center = CGPointMake(image.frame.size.width - 5 + label.frame.size.width * 0.5 + 5, self.frame.size.height * 0.5); } } if (self.isSelected) { CALayer *layer = label.layer; layer.cornerRadius = label.frame.size.height * 0.5 - 2; layer.masksToBounds = YES; } } } } - (void)setHighlighted:(BOOL)highlighted { } - (UIColor *) colorWithHexString: (NSString *) hexString { NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString]; CGFloat alpha, red, blue, green; switch ([colorString length]) { case 3: // #RGB alpha = 1.0f; red = [self colorComponentFrom: colorString start: 0 length: 1]; green = [self colorComponentFrom: colorString start: 1 length: 1]; blue = [self colorComponentFrom: colorString start: 2 length: 1]; break; case 4: // #ARGB alpha = [self colorComponentFrom: colorString start: 0 length: 1]; red = [self colorComponentFrom: colorString start: 1 length: 1]; green = [self colorComponentFrom: colorString start: 2 length: 1]; blue = [self colorComponentFrom: colorString start: 3 length: 1]; break; case 6: // #RRGGBB alpha = 1.0f; red = [self colorComponentFrom: colorString start: 0 length: 2]; green = [self colorComponentFrom: colorString start: 2 length: 2]; blue = [self colorComponentFrom: colorString start: 4 length: 2]; break; case 8: // #AARRGGBB alpha = [self colorComponentFrom: colorString start: 0 length: 2]; red = [self colorComponentFrom: colorString start: 2 length: 2]; green = [self colorComponentFrom: colorString start: 4 length: 2]; blue = [self colorComponentFrom: colorString start: 6 length: 2]; break; default: [NSException raise:@"Invalid color value" format: @"Color value %@ is invalid. It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString]; break; } return [UIColor colorWithRed: red green: green blue: blue alpha: alpha]; } - (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length { NSString *substring = [string substringWithRange: NSMakeRange(start, length)]; NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring]; unsigned hexComponent; [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent]; return hexComponent / 255.0; } @end