-
Notifications
You must be signed in to change notification settings - Fork 683
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
9 changed files
with
794 additions
and
1 deletion.
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,54 @@ | ||
// | ||
// MPWeakTimer.h 破坏nstimer内存泄露的问题 | ||
// MobileProject | ||
// | ||
//使用说明:就可以在dealloc里面进行invalidate | ||
//#import "HWWeakTimer.h" | ||
// | ||
//@interface DetailViewController () | ||
//@property (nonatomic, weak) NSTimer *timer; | ||
// | ||
//@end | ||
// | ||
//@implementation DetailViewController | ||
// | ||
//- (IBAction)fireButtonPressed:(id)sender { | ||
// _timer = [HWWeakTimer scheduledTimerWithTimeInterval:3.0f block:^(id userInfo) { | ||
// NSLog(@"%@", userInfo); | ||
// } userInfo:@"Fire" repeats:YES]; | ||
// [_timer fire]; | ||
//} | ||
// | ||
//- (IBAction)invalidateButtonPressed:(id)sender { | ||
// [_timer invalidate]; | ||
//} | ||
// | ||
//-(void)dealloc { | ||
// [_timer invalidate]; | ||
// NSLog(@"%@ dealloc", NSStringFromClass([self class])); | ||
//} | ||
// | ||
//@end | ||
// | ||
// Created by wujunyang on 2017/5/12. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
typedef void (^MPTimerHandler)(id userInfo); | ||
|
||
@interface MPWeakTimer : NSObject | ||
|
||
+ (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval | ||
target:(id)aTarget | ||
selector:(SEL)aSelector | ||
userInfo:(id)userInfo | ||
repeats:(BOOL)repeats; | ||
|
||
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval | ||
block:(MPTimerHandler)block | ||
userInfo:(id)userInfo | ||
repeats:(BOOL)repeats; | ||
|
||
@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,80 @@ | ||
// | ||
// MPWeakTimer.m | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/5/12. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import "MPWeakTimer.h" | ||
|
||
@interface MPWeakTimerTarget : NSObject | ||
|
||
@property (nonatomic, weak) id target; | ||
@property (nonatomic, assign) SEL selector; | ||
@property (nonatomic, weak) NSTimer* timer; | ||
|
||
@end | ||
|
||
@implementation MPWeakTimerTarget | ||
|
||
- (void) fire:(NSTimer *)timer { | ||
if(self.target) { | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Warc-performSelector-leaks" | ||
[self.target performSelector:self.selector withObject:timer.userInfo afterDelay:0.0f]; | ||
#pragma clang diagnostic pop | ||
} else { | ||
[self.timer invalidate]; | ||
} | ||
} | ||
|
||
@end | ||
|
||
@implementation MPWeakTimer | ||
|
||
+ (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval | ||
target:(id)aTarget | ||
selector:(SEL)aSelector | ||
userInfo:(id)userInfo | ||
repeats:(BOOL)repeats { | ||
MPWeakTimerTarget* timerTarget = [[MPWeakTimerTarget alloc] init]; | ||
timerTarget.target = aTarget; | ||
timerTarget.selector = aSelector; | ||
timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:interval | ||
target:timerTarget | ||
selector:@selector(fire:) | ||
userInfo:userInfo | ||
repeats:repeats]; | ||
return timerTarget.timer; | ||
} | ||
|
||
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval | ||
block:(MPTimerHandler)block | ||
userInfo:(id)userInfo | ||
repeats:(BOOL)repeats { | ||
NSMutableArray *userInfoArray = [NSMutableArray arrayWithObject:[block copy]]; | ||
if (userInfo != nil) { | ||
[userInfoArray addObject:userInfo]; | ||
} | ||
return [self scheduledTimerWithTimeInterval:interval | ||
target:self | ||
selector:@selector(_timerBlockInvoke:) | ||
userInfo:[userInfoArray copy] | ||
repeats:repeats]; | ||
} | ||
|
||
+ (void)_timerBlockInvoke:(NSArray*)userInfo { | ||
MPTimerHandler block = userInfo[0]; | ||
id info = nil; | ||
if (userInfo.count == 2) { | ||
info = userInfo[1]; | ||
} | ||
// or `!block ?: block();` @sunnyxx | ||
if (block) { | ||
block(info); | ||
} | ||
} | ||
|
||
|
||
@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
14 changes: 14 additions & 0 deletions
14
MobileProject/Main/Preview/Controller/AudioVideoController/MPvideoRecordingViewController.h
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,14 @@ | ||
// | ||
// MPvideoRecordingViewController.h | ||
// MobileProject | ||
// | ||
// Created by wujunyang on 2017/5/10. | ||
// Copyright © 2017年 wujunyang. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import "BaseViewController.h" | ||
|
||
@interface MPvideoRecordingViewController : BaseViewController | ||
|
||
@end |
Oops, something went wrong.