New file |
| | |
| | | // |
| | | // YYFPSLabel.m |
| | | // YYKitExample |
| | | // |
| | | // Created by ibireme on 15/9/3. |
| | | // Copyright (c) 2015 ibireme. All rights reserved. |
| | | // |
| | | |
| | | #import "YYFPSLabel.h" |
| | | |
| | | #define kSize CGSizeMake(55, 20) |
| | | |
| | | @implementation YYFPSLabel { |
| | | CADisplayLink *_link; |
| | | NSUInteger _count; |
| | | NSTimeInterval _lastTime; |
| | | UIFont *_font; |
| | | UIFont *_subFont; |
| | | NSTimeInterval _llll; |
| | | } |
| | | |
| | | - (instancetype)initWithFrame:(CGRect)frame { |
| | | if (frame.size.width == 0 && frame.size.height == 0) { |
| | | frame.size = kSize; |
| | | } |
| | | self = [super initWithFrame:frame]; |
| | | |
| | | self.layer.cornerRadius = 5; |
| | | self.clipsToBounds = YES; |
| | | self.textAlignment = NSTextAlignmentCenter; |
| | | self.userInteractionEnabled = YES; |
| | | self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.700]; |
| | | |
| | | _font = [UIFont fontWithName:@"Menlo" size:14]; |
| | | if (_font) { |
| | | _subFont = [UIFont fontWithName:@"Menlo" size:4]; |
| | | } else { |
| | | _font = [UIFont fontWithName:@"Courier" size:14]; |
| | | _subFont = [UIFont fontWithName:@"Courier" size:4]; |
| | | } |
| | | |
| | | __weak typeof(self) weakSelf = self; |
| | | _link = [CADisplayLink displayLinkWithTarget:weakSelf selector:@selector(tick:)]; |
| | | [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; |
| | | |
| | | [self addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithActionBlock:^(UIPanGestureRecognizer *_Nonnull sender) { |
| | | |
| | | // 获取手势的移动,也是相对于最开始的位置 |
| | | CGPoint transP = [sender translationInView:weakSelf]; |
| | | |
| | | weakSelf.transform = CGAffineTransformTranslate(weakSelf.transform, transP.x, transP.y); |
| | | |
| | | // 复位 |
| | | [sender setTranslation:CGPointZero inView:weakSelf]; |
| | | |
| | | if (sender.state == UIGestureRecognizerStateEnded) { |
| | | |
| | | [UIView animateWithDuration:0.2 animations:^{ |
| | | weakSelf.lmj_x = (weakSelf.lmj_x - kScreenWidth * 0.5) > 0 ? (kScreenWidth - weakSelf.lmj_width - 20) : 20; |
| | | weakSelf.lmj_y = weakSelf.lmj_y > 80 ? weakSelf.lmj_y : 80; |
| | | }]; |
| | | } |
| | | |
| | | }]]; |
| | | |
| | | return self; |
| | | } |
| | | |
| | | - (void)dealloc { |
| | | [_link invalidate]; |
| | | _link = nil; |
| | | } |
| | | |
| | | - (void)tick:(CADisplayLink *)link { |
| | | if (_lastTime == 0) { |
| | | _lastTime = link.timestamp; |
| | | return; |
| | | } |
| | | _count++; |
| | | NSTimeInterval delta = link.timestamp - _lastTime; |
| | | if (delta < 1) return; |
| | | _lastTime = link.timestamp; |
| | | CGFloat fps = _count / delta; |
| | | _count = 0; |
| | | |
| | | CGFloat progress = fps / 60.0; |
| | | UIColor *color = [UIColor colorWithHue:0.27 * (progress - 0.2) saturation:1 brightness:0.9 alpha:1]; |
| | | |
| | | NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%zd FPS",(int)round(fps)]]; |
| | | |
| | | [text yy_setColor:color range:NSMakeRange(0, text.length - 3)]; |
| | | [text yy_setColor:[UIColor whiteColor] range:NSMakeRange(text.length - 3, 3)]; |
| | | text.yy_font = _font; |
| | | [text yy_setFont:_subFont range:NSMakeRange(text.length - 4, 1)]; |
| | | |
| | | self.attributedText = text; |
| | | } |
| | | |
| | | @end |