AnimOperation.m
2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//
// AnimOperation.m
// presentAnimation
//
// Created by 许博 on 16/7/15.
// Copyright © 2016年 许博. All rights reserved.
//
#import "AnimOperation.h"
#define KScreenWidth [[UIScreen mainScreen] bounds].size.width
@interface AnimOperation ()
@property (nonatomic, getter = isFinished) BOOL finished;
@property (nonatomic, getter = isExecuting) BOOL executing;
@property (nonatomic,copy) void(^finishedBlock)(BOOL result,NSInteger finishCount,NSInteger finishGiftCount);
@end
@implementation AnimOperation
@synthesize finished = _finished;
@synthesize executing = _executing;
+ (instancetype)animOperationWithUserID:(NSString *)userID finishGiftCount:(NSInteger)finishGiftCount model:(GiftModel *)model finishedBlock:(void(^)(BOOL result,NSInteger finishCount,NSInteger finishGiftCount))finishedBlock
{
AnimOperation *op = [[AnimOperation alloc] init];
op.presentView = [[PresentView alloc] init];
op.model = model;
op.finishedBlock = finishedBlock;
return op;
}
- (instancetype)init
{
self = [super init];
if (self) {
_executing = NO;
_finished = NO;
}
return self;
}
- (void)start {
// 添加到队列时调用
// if (如果半天没消息或者取消了操作) {
// return
// }
// self.executing = YES;
// [[NSOperationQueue mainQueue] addOperationWithBlock:^{
// 执行动画;
// } completion:^(BOOL finished) {
// self.finished = YES;
// self.executing = NO;
// }];
// }];
if ([self isCancelled]) {
self.finished = YES;
return;
}
self.executing = YES;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
_presentView.model = _model;
// i % 2 控制最多允许出现几行
if (_index % 2) {
_presentView.frame = CGRectMake(-self.listView.frame.size.width / 2, 300, self.listView.frame.size.width / 2, 40);
}else {
_presentView.frame = CGRectMake(-self.listView.frame.size.width / 2, 230, self.listView.frame.size.width / 2, 40);
}
_presentView.originFrame = _presentView.frame;
[self.listView addSubview:_presentView];
[self.presentView animateWithCompleteBlock:^(BOOL finished,NSInteger finishCount,NSInteger finishGiftCount) {
self.finished = finished;
self.finishedBlock(finished,finishCount,finishGiftCount);
}];
}];
}
#pragma mark - 手动触发 KVO
- (void)setExecuting:(BOOL)executing
{
[self willChangeValueForKey:@"isExecuting"];
_executing = executing;
[self didChangeValueForKey:@"isExecuting"];
}
- (void)setFinished:(BOOL)finished
{
[self willChangeValueForKey:@"isFinished"];
_finished = finished;
[self didChangeValueForKey:@"isFinished"];
}
@end