From 557c84d2d45a405ff2135c8143e496a9458ae118 Mon Sep 17 00:00:00 2001 From: KANGZUBIN Date: Fri, 4 May 2018 21:33:35 +0800 Subject: [PATCH] Add FPS Check for Demo --- .../project.pbxproj | 20 +++++ Demo/XMNetworkingDemo/AppDelegate.m | 10 +++ Demo/XMNetworkingDemo/YYFPSLabel/YYFPSLabel.h | 20 +++++ Demo/XMNetworkingDemo/YYFPSLabel/YYFPSLabel.m | 82 ++++++++++++++++++ .../XMNetworkingDemo/YYFPSLabel/YYWeakProxy.h | 57 +++++++++++++ .../XMNetworkingDemo/YYFPSLabel/YYWeakProxy.m | 83 +++++++++++++++++++ 6 files changed, 272 insertions(+) create mode 100644 Demo/XMNetworkingDemo/YYFPSLabel/YYFPSLabel.h create mode 100644 Demo/XMNetworkingDemo/YYFPSLabel/YYFPSLabel.m create mode 100644 Demo/XMNetworkingDemo/YYFPSLabel/YYWeakProxy.h create mode 100644 Demo/XMNetworkingDemo/YYFPSLabel/YYWeakProxy.m diff --git a/Demo/XMNetworkingDemo.xcodeproj/project.pbxproj b/Demo/XMNetworkingDemo.xcodeproj/project.pbxproj index 1a4857f..563a09a 100644 --- a/Demo/XMNetworkingDemo.xcodeproj/project.pbxproj +++ b/Demo/XMNetworkingDemo.xcodeproj/project.pbxproj @@ -29,6 +29,8 @@ 4BF801101DFED40000999C54 /* XMRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF8010F1DFED40000999C54 /* XMRequest.m */; }; 4BF801131DFED44300999C54 /* XMCenter.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF801121DFED44300999C54 /* XMCenter.m */; }; 4BF801161DFED49500999C54 /* XMEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 4BF801151DFED49500999C54 /* XMEngine.m */; }; + 897281C4209C926D001799A9 /* YYFPSLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 897281C1209C926D001799A9 /* YYFPSLabel.m */; }; + 897281C5209C926D001799A9 /* YYWeakProxy.m in Sources */ = {isa = PBXBuildFile; fileRef = 897281C3209C926D001799A9 /* YYWeakProxy.m */; }; 9017F63AE450E7282A414025 /* libPods-XMNetworkingDemo.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6DDAEF2FD9EBFD973FD5002C /* libPods-XMNetworkingDemo.a */; }; /* End PBXBuildFile section */ @@ -83,6 +85,10 @@ 4BF801151DFED49500999C54 /* XMEngine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMEngine.m; sourceTree = ""; }; 4F2F058D11AEAA8CCBA69787 /* Pods-XMNetworkingDemo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-XMNetworkingDemo.debug.xcconfig"; path = "Pods/Target Support Files/Pods-XMNetworkingDemo/Pods-XMNetworkingDemo.debug.xcconfig"; sourceTree = ""; }; 6DDAEF2FD9EBFD973FD5002C /* libPods-XMNetworkingDemo.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-XMNetworkingDemo.a"; sourceTree = BUILT_PRODUCTS_DIR; }; + 897281C0209C926D001799A9 /* YYFPSLabel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YYFPSLabel.h; sourceTree = ""; }; + 897281C1209C926D001799A9 /* YYFPSLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YYFPSLabel.m; sourceTree = ""; }; + 897281C2209C926D001799A9 /* YYWeakProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YYWeakProxy.h; sourceTree = ""; }; + 897281C3209C926D001799A9 /* YYWeakProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YYWeakProxy.m; sourceTree = ""; }; A0DF4180EF91D6CD7F45799D /* Pods-XMNetworkingDemo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-XMNetworkingDemo.release.xcconfig"; path = "Pods/Target Support Files/Pods-XMNetworkingDemo/Pods-XMNetworkingDemo.release.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -138,6 +144,7 @@ 4BF800CC1DFED15C00999C54 /* XMNetworkingDemo */ = { isa = PBXGroup; children = ( + 897281BF209C926D001799A9 /* YYFPSLabel */, 4BF800D01DFED15C00999C54 /* AppDelegate.h */, 4BF800D11DFED15C00999C54 /* AppDelegate.m */, 4B16DFA5208B7E4B00BF7AC5 /* NetworkManager.h */, @@ -201,6 +208,17 @@ path = ../XMNetworking; sourceTree = ""; }; + 897281BF209C926D001799A9 /* YYFPSLabel */ = { + isa = PBXGroup; + children = ( + 897281C0209C926D001799A9 /* YYFPSLabel.h */, + 897281C1209C926D001799A9 /* YYFPSLabel.m */, + 897281C2209C926D001799A9 /* YYWeakProxy.h */, + 897281C3209C926D001799A9 /* YYWeakProxy.m */, + ); + path = YYFPSLabel; + sourceTree = ""; + }; F4ADCB8B8390E1D937ED91BE /* Frameworks */ = { isa = PBXGroup; children = ( @@ -339,10 +357,12 @@ 4BF801101DFED40000999C54 /* XMRequest.m in Sources */, 4BF801131DFED44300999C54 /* XMCenter.m in Sources */, 4B16DFB0208C94E500BF7AC5 /* SearchViewController.m in Sources */, + 897281C4209C926D001799A9 /* YYFPSLabel.m in Sources */, 4B16DFAA208B7FFB00BF7AC5 /* TipSetTableViewCell.m in Sources */, 4BF800D21DFED15C00999C54 /* AppDelegate.m in Sources */, 4B16DFAD208B89F100BF7AC5 /* TipSetItemModel.m in Sources */, 4BD09BC62087A789003591F4 /* MainViewController.m in Sources */, + 897281C5209C926D001799A9 /* YYWeakProxy.m in Sources */, 4BF800CF1DFED15C00999C54 /* main.m in Sources */, 4BF801161DFED49500999C54 /* XMEngine.m in Sources */, 4B16DFA7208B7E4B00BF7AC5 /* NetworkManager.m in Sources */, diff --git a/Demo/XMNetworkingDemo/AppDelegate.m b/Demo/XMNetworkingDemo/AppDelegate.m index 9279caf..a1b9f81 100644 --- a/Demo/XMNetworkingDemo/AppDelegate.m +++ b/Demo/XMNetworkingDemo/AppDelegate.m @@ -9,6 +9,10 @@ #import "AppDelegate.h" #import "MainViewController.h" +#ifdef DEBUG +#import "YYFPSLabel.h" +#endif + @interface AppDelegate () @end @@ -25,6 +29,12 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( self.window.rootViewController = rootNavigationController; [self.window makeKeyAndVisible]; +#ifdef DEBUG + YYFPSLabel *fpsLabel = [[YYFPSLabel alloc] init]; + [self.window addSubview:fpsLabel]; + fpsLabel.center = self.window.center; +#endif + return YES; } diff --git a/Demo/XMNetworkingDemo/YYFPSLabel/YYFPSLabel.h b/Demo/XMNetworkingDemo/YYFPSLabel/YYFPSLabel.h new file mode 100644 index 0000000..16b416b --- /dev/null +++ b/Demo/XMNetworkingDemo/YYFPSLabel/YYFPSLabel.h @@ -0,0 +1,20 @@ +// +// YYFPSLabel.h +// YYKitExample +// +// Created by ibireme on 15/9/3. +// Copyright (c) 2015 ibireme. All rights reserved. +// + +#import + +/** + 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 diff --git a/Demo/XMNetworkingDemo/YYFPSLabel/YYFPSLabel.m b/Demo/XMNetworkingDemo/YYFPSLabel/YYFPSLabel.m new file mode 100644 index 0000000..5aab878 --- /dev/null +++ b/Demo/XMNetworkingDemo/YYFPSLabel/YYFPSLabel.m @@ -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 diff --git a/Demo/XMNetworkingDemo/YYFPSLabel/YYWeakProxy.h b/Demo/XMNetworkingDemo/YYFPSLabel/YYWeakProxy.h new file mode 100644 index 0000000..aa49b67 --- /dev/null +++ b/Demo/XMNetworkingDemo/YYFPSLabel/YYWeakProxy.h @@ -0,0 +1,57 @@ +// +// YYWeakProxy.h +// 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 + +/** + 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 diff --git a/Demo/XMNetworkingDemo/YYFPSLabel/YYWeakProxy.m b/Demo/XMNetworkingDemo/YYFPSLabel/YYWeakProxy.m new file mode 100644 index 0000000..fbf648d --- /dev/null +++ b/Demo/XMNetworkingDemo/YYFPSLabel/YYWeakProxy.m @@ -0,0 +1,83 @@ +// +// YYWeakProxy.m +// 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