Skip to content

Commit

Permalink
2nd push
Browse files Browse the repository at this point in the history
add project --runLoopDemo
  • Loading branch information
wustzhy committed Jan 3, 2017
1 parent 10273f8 commit 5497834
Show file tree
Hide file tree
Showing 21 changed files with 1,501 additions and 0 deletions.
454 changes: 454 additions & 0 deletions RunLoopDemo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions RunLoopDemo/Base.lproj/LaunchScreen.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6214" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6207"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="iN0-l3-epB">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" Copyright (c) 2014 Chun Tips. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
<rect key="frame" x="20" y="439" width="441" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="RunLoopDemo" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
<rect key="frame" x="20" y="140" width="441" height="43"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" constant="1" id="5cJ-9S-tgC"/>
<constraint firstAttribute="centerX" secondItem="kId-c2-rCX" secondAttribute="centerX" id="Koa-jz-hwk"/>
<constraint firstAttribute="bottom" secondItem="8ie-xW-0ye" secondAttribute="bottom" constant="20" id="Kzo-t9-V3l"/>
<constraint firstItem="8ie-xW-0ye" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="MfP-vx-nX0"/>
<constraint firstAttribute="centerX" secondItem="8ie-xW-0ye" secondAttribute="centerX" id="ZEH-qu-HZ9"/>
<constraint firstItem="kId-c2-rCX" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="fvb-Df-36g"/>
</constraints>
<nil key="simulatedStatusBarMetrics"/>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<point key="canvasLocation" x="548" y="455"/>
</view>
</objects>
</document>
26 changes: 26 additions & 0 deletions RunLoopDemo/CCAppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// AppDelegate.h
// RunLoopDemo
//
// Created by Chun Ye on 10/20/14.
// Copyright (c) 2014 Chun Tips. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "CCRunLoopInputSource.h"

@interface CCAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

@interface CCAppDelegate (RunLoop)

- (void)registerSource:(CCRunLoopContext *)sourceContext;

- (void)removeSource:(CCRunLoopContext *)sourceContext;

- (void)testInputSourceEvent;

@end
93 changes: 93 additions & 0 deletions RunLoopDemo/CCAppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// AppDelegate.m
// RunLoopDemo
//
// Created by Chun Ye on 10/20/14.
// Copyright (c) 2014 Chun Tips. All rights reserved.
//

#import "CCAppDelegate.h"
#import "CCTestRunLoopViewController.h"
#import "CCRunLoopThread.h"
#import "CCRunLoopCustomInputSourceThread.h"

#define kTestRunLoopThread 0
#define kTestCustomInputSpurceRunLoopThread 1

@interface CCAppDelegate ()

@property (nonatomic, strong) NSMutableArray *sources;

@end

@implementation CCAppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];

CCTestRunLoopViewController *testViewController = [[CCTestRunLoopViewController alloc] init];
UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:testViewController];
self.window.rootViewController = nav;

[self.window makeKeyAndVisible];

if (kTestRunLoopThread) {
[self startRunLoopThread];
}

if (kTestCustomInputSpurceRunLoopThread) {
[self startCustomInputSpurceRunLoopThread];
}

return YES;
}

#pragma mark - Private

- (void)startRunLoopThread
{
CCRunLoopThread *runLoopThread = [[CCRunLoopThread alloc] init];
[runLoopThread start];
}

- (void)startCustomInputSpurceRunLoopThread
{
CCRunLoopCustomInputSourceThread *customInputSourceThread = [[CCRunLoopCustomInputSourceThread alloc] init];
[customInputSourceThread start];
}

@end

@implementation CCAppDelegate (RunLoop)

- (void)registerSource:(CCRunLoopContext *)sourceContext
{
if (!self.sources) {
self.sources = [NSMutableArray array];
}
[self.sources addObject:sourceContext];
}

- (void)removeSource:(CCRunLoopContext *)sourceContext
{
[self.sources enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CCRunLoopContext *context = obj;
if ([context isEqual:sourceContext]) {
[self.sources removeObject:context];
*stop = YES;
}
}];
}

- (void)testInputSourceEvent
{
CCRunLoopContext *runLoopContext = [self.sources objectAtIndex:0];
CCRunLoopInputSource *inputSource = runLoopContext.runLoopInputSource;
[inputSource addTestPrintCommandWithString:[[NSDate date] description]];
[inputSource fireAllCommandsOnRunLoop:runLoopContext.runLoop];
}

@end
13 changes: 13 additions & 0 deletions RunLoopDemo/CCRunLoopCustomInputSourceThread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// CCRunLoopCustomInputSourceThread.h
// RunLoopDemo
//
// Created by Chun Ye on 10/20/14.
// Copyright (c) 2014 Chun Tips. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CCRunLoopCustomInputSourceThread : NSThread

@end
61 changes: 61 additions & 0 deletions RunLoopDemo/CCRunLoopCustomInputSourceThread.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// CCRunLoopCustomInputSourceThread.m
// RunLoopDemo
//
// Created by Chun Ye on 10/20/14.
// Copyright (c) 2014 Chun Tips. All rights reserved.
//

#import "CCRunLoopCustomInputSourceThread.h"
#import "CCRunLoopInputSource.h"

@interface CCRunLoopCustomInputSourceThread () <CCRunLoopInputSourceTestDelegate>

@property (nonatomic, strong) CCRunLoopInputSource *customInputSource;

@end

@implementation CCRunLoopCustomInputSourceThread

- (void)main
{
@autoreleasepool {
NSLog(@"CCRunLoopCustomInputSourceThread Enter");

NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];

self.customInputSource = [[CCRunLoopInputSource alloc] init];
self.customInputSource.delegate = self;
[self.customInputSource addToCurrentRunLoop];

while (!self.cancelled) {
NSLog(@"Enter Run Loop");

// print test string;
[self finishOtherTask];

[currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

NSLog(@"Exit Run Loop");
}

NSLog(@"CCRunLoopCustomInputSourceThread Exit");
}
}

- (void)finishOtherTask
{
NSLog(@"Begin finishOtherTask");
NSLog(@"🌹🌹🌹🌹🌹🌹🌹🌹🌹🌹🌹");
NSLog(@"🌹🌹🌹🌹🌹🌹🌹🌹🌹🌹🌹");
NSLog(@"End finishOtherTask");
}

#pragma mark - CCRunLoopInputSourceTestDelegate

- (void)activeInputSourceForTestPrintStringEvent:(NSString *)string
{
NSLog(@"activeInputSourceForTestPrintStringEvent : %@", string);
}

@end
46 changes: 46 additions & 0 deletions RunLoopDemo/CCRunLoopInputSource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// CCRunLoopInputSource.h
// RunLoopDemo
//
// Created by Chun Ye on 10/20/14.
// Copyright (c) 2014 Chun Tips. All rights reserved.
//

#import <Foundation/Foundation.h>

@protocol CCRunLoopInputSourceTestDelegate <NSObject>

- (void)activeInputSourceForTestPrintStringEvent:(NSString *)string;

@end

@interface CCRunLoopInputSource : NSObject


@property (nonatomic, weak) id <CCRunLoopInputSourceTestDelegate> delegate;

// 初始化和销毁
- (instancetype)init;
- (void)addToCurrentRunLoop;
- (void)invalidate;

// 处理事件
- (void)inputSourceFired;

// 其他线程注册事件
- (void)addCommand:(NSInteger)command data:(NSData *)data;
- (void)addTestPrintCommandWithString:(NSString *)string;

- (void)fireAllCommandsOnRunLoop:(CFRunLoopRef)runLoop;

@end

// 容器类,用来保存和传递数据
@interface CCRunLoopContext : NSObject

@property (nonatomic, readonly) CFRunLoopRef runLoop;
@property (nonatomic, readonly) CCRunLoopInputSource *runLoopInputSource;

- (instancetype)initWithSource:(CCRunLoopInputSource *)runLoopInputSource runLoop:(CFRunLoopRef)runLoop;

@end
Loading

0 comments on commit 5497834

Please sign in to comment.