Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
272 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// YYFPSLabel.h | ||
// YYKitExample | ||
// | ||
// Created by ibireme on 15/9/3. | ||
// Copyright (c) 2015 ibireme. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
/** | ||
Show Screen FPS... | ||
The maximum fps in OSX/iOS Simulator is 60.00. | ||
The maximum fps on iPhone is 59.97. | ||
The maxmium fps on iPad is 60.0. | ||
*/ | ||
@interface YYFPSLabel : UILabel | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// YYFPSLabel.m | ||
// YYKitExample | ||
// | ||
// Created by ibireme on 15/9/3. | ||
// Copyright (c) 2015 ibireme. All rights reserved. | ||
// | ||
|
||
#import "YYFPSLabel.h" | ||
#import "YYWeakProxy.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 = NO; | ||
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]; | ||
} | ||
|
||
_link = [CADisplayLink displayLinkWithTarget:[YYWeakProxy proxyWithTarget:self] selector:@selector(tick:)]; | ||
[_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes]; | ||
return self; | ||
} | ||
|
||
- (void)dealloc { | ||
[_link invalidate]; | ||
} | ||
|
||
- (CGSize)sizeThatFits:(CGSize)size { | ||
return kSize; | ||
} | ||
|
||
- (void)tick:(CADisplayLink *)link { | ||
if (_lastTime == 0) { | ||
_lastTime = link.timestamp; | ||
return; | ||
} | ||
|
||
_count++; | ||
NSTimeInterval delta = link.timestamp - _lastTime; | ||
if (delta < 1) return; | ||
_lastTime = link.timestamp; | ||
float 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:@"%d FPS",(int)round(fps)]]; | ||
[text addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, text.length - 3)]; | ||
[text addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(text.length - 3, 3)]; | ||
[text addAttribute:NSFontAttributeName value:_font range:NSMakeRange(0, text.length)]; | ||
[text addAttribute:NSFontAttributeName value:_subFont range:NSMakeRange(text.length - 4, 1)]; | ||
|
||
self.attributedText = text; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// YYWeakProxy.h | ||
// YYKit <https://github.com/ibireme/YYKit> | ||
// | ||
// Created by ibireme on 14/10/18. | ||
// Copyright (c) 2015 ibireme. | ||
// | ||
// This source code is licensed under the MIT-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
/** | ||
A proxy used to hold a weak object. | ||
It can be used to avoid retain cycles, such as the target in NSTimer or CADisplayLink. | ||
sample code: | ||
@implementation MyView { | ||
NSTimer *_timer; | ||
} | ||
- (void)initTimer { | ||
YYWeakProxy *proxy = [YYWeakProxy proxyWithTarget:self]; | ||
_timer = [NSTimer timerWithTimeInterval:0.1 target:proxy selector:@selector(tick:) userInfo:nil repeats:YES]; | ||
} | ||
- (void)tick:(NSTimer *)timer {...} | ||
@end | ||
*/ | ||
@interface YYWeakProxy : NSProxy | ||
|
||
/** | ||
The proxy target. | ||
*/ | ||
@property (nonatomic, weak, readonly) id target; | ||
|
||
/** | ||
Creates a new weak proxy for target. | ||
@param target Target object. | ||
@return A new proxy object. | ||
*/ | ||
- (instancetype)initWithTarget:(id)target; | ||
|
||
/** | ||
Creates a new weak proxy for target. | ||
@param target Target object. | ||
@return A new proxy object. | ||
*/ | ||
+ (instancetype)proxyWithTarget:(id)target; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// YYWeakProxy.m | ||
// YYKit <https://github.com/ibireme/YYKit> | ||
// | ||
// Created by ibireme on 14/10/18. | ||
// Copyright (c) 2015 ibireme. | ||
// | ||
// This source code is licensed under the MIT-style license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
// | ||
|
||
#import "YYWeakProxy.h" | ||
|
||
|
||
@implementation YYWeakProxy | ||
|
||
- (instancetype)initWithTarget:(id)target { | ||
_target = target; | ||
return self; | ||
} | ||
|
||
+ (instancetype)proxyWithTarget:(id)target { | ||
return [[YYWeakProxy alloc] initWithTarget:target]; | ||
} | ||
|
||
- (id)forwardingTargetForSelector:(SEL)selector { | ||
return _target; | ||
} | ||
|
||
- (void)forwardInvocation:(NSInvocation *)invocation { | ||
void *null = NULL; | ||
[invocation setReturnValue:&null]; | ||
} | ||
|
||
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector { | ||
return [NSObject instanceMethodSignatureForSelector:@selector(init)]; | ||
} | ||
|
||
- (BOOL)respondsToSelector:(SEL)aSelector { | ||
return [_target respondsToSelector:aSelector]; | ||
} | ||
|
||
- (BOOL)isEqual:(id)object { | ||
return [_target isEqual:object]; | ||
} | ||
|
||
- (NSUInteger)hash { | ||
return [_target hash]; | ||
} | ||
|
||
- (Class)superclass { | ||
return [_target superclass]; | ||
} | ||
|
||
- (Class)class { | ||
return [_target class]; | ||
} | ||
|
||
- (BOOL)isKindOfClass:(Class)aClass { | ||
return [_target isKindOfClass:aClass]; | ||
} | ||
|
||
- (BOOL)isMemberOfClass:(Class)aClass { | ||
return [_target isMemberOfClass:aClass]; | ||
} | ||
|
||
- (BOOL)conformsToProtocol:(Protocol *)aProtocol { | ||
return [_target conformsToProtocol:aProtocol]; | ||
} | ||
|
||
- (BOOL)isProxy { | ||
return YES; | ||
} | ||
|
||
- (NSString *)description { | ||
return [_target description]; | ||
} | ||
|
||
- (NSString *)debugDescription { | ||
return [_target debugDescription]; | ||
} | ||
|
||
@end |