Skip to content

Commit

Permalink
Add FPS Check for Demo
Browse files Browse the repository at this point in the history
  • Loading branch information
kangzubin committed May 4, 2018
1 parent af97f36 commit 557c84d
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Demo/XMNetworkingDemo.xcodeproj/project.pbxproj
Expand Up @@ -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 */

Expand Down Expand Up @@ -83,6 +85,10 @@
4BF801151DFED49500999C54 /* XMEngine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMEngine.m; sourceTree = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
897281C1209C926D001799A9 /* YYFPSLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YYFPSLabel.m; sourceTree = "<group>"; };
897281C2209C926D001799A9 /* YYWeakProxy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YYWeakProxy.h; sourceTree = "<group>"; };
897281C3209C926D001799A9 /* YYWeakProxy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YYWeakProxy.m; sourceTree = "<group>"; };
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 = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -138,6 +144,7 @@
4BF800CC1DFED15C00999C54 /* XMNetworkingDemo */ = {
isa = PBXGroup;
children = (
897281BF209C926D001799A9 /* YYFPSLabel */,
4BF800D01DFED15C00999C54 /* AppDelegate.h */,
4BF800D11DFED15C00999C54 /* AppDelegate.m */,
4B16DFA5208B7E4B00BF7AC5 /* NetworkManager.h */,
Expand Down Expand Up @@ -201,6 +208,17 @@
path = ../XMNetworking;
sourceTree = "<group>";
};
897281BF209C926D001799A9 /* YYFPSLabel */ = {
isa = PBXGroup;
children = (
897281C0209C926D001799A9 /* YYFPSLabel.h */,
897281C1209C926D001799A9 /* YYFPSLabel.m */,
897281C2209C926D001799A9 /* YYWeakProxy.h */,
897281C3209C926D001799A9 /* YYWeakProxy.m */,
);
path = YYFPSLabel;
sourceTree = "<group>";
};
F4ADCB8B8390E1D937ED91BE /* Frameworks */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -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 */,
Expand Down
10 changes: 10 additions & 0 deletions Demo/XMNetworkingDemo/AppDelegate.m
Expand Up @@ -9,6 +9,10 @@
#import "AppDelegate.h"
#import "MainViewController.h"

#ifdef DEBUG
#import "YYFPSLabel.h"
#endif

@interface AppDelegate ()

@end
Expand All @@ -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;
}

Expand Down
20 changes: 20 additions & 0 deletions 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 <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
82 changes: 82 additions & 0 deletions 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
57 changes: 57 additions & 0 deletions Demo/XMNetworkingDemo/YYFPSLabel/YYWeakProxy.h
@@ -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
83 changes: 83 additions & 0 deletions Demo/XMNetworkingDemo/YYFPSLabel/YYWeakProxy.m
@@ -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

0 comments on commit 557c84d

Please sign in to comment.