nciagra (owner)

Forks

Revisions

gist: 151545 Download_button fork
public
Description:
CPPropertyAnimation
Public Clone URL: git://gist.github.com/151545.git
Embed All Files: show embed
CPPropertyAnimation.j #
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* CPPropertyAnimation.j
* AppKit
*
* Created by Nicholas Small. (Extended by David Cann).
* Copyright 2009, Nicholas Small.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
 
/*
A VERY basic, but functional, property for Cappuccino. This is not CoreAnimation,
nor anything similar. However, it will handle some basic property animations, and
more can easily be added.
*/
 
@implementation CPPropertyAnimation : CPAnimation
{
CPView view;
CPDictionary properties;
 
CPView _startView;
BOOL _endView;
}
 
- (id)initWithView:(CPView)aView
{
self = [super initWithDuration:1.0 animationCurve:CPAnimationLinear];
 
if (self)
{
[self setDelegate:self];
 
view = aView;
properties = [CPDictionary dictionary];
}
 
return self;
}
 
- (CPView)view
{
return view;
}
 
- (void)addProperty:(CPString)aPath start:(CPValue)aStart end:(CPValue)anEnd
{
if (![view respondsToSelector:aPath])
return;
 
[properties setObject:{start: aStart, end:anEnd} forKey:aPath];
 
[view setValue:aStart forKey:aPath];
}
 
- (void)addToViewOnStart:(CPView)aView
{
_startView = aView;
}
 
- (CPView)willAddToViewOnStart
{
return _startView;
}
 
- (void)removeFromSuperviewOnEnd:(BOOL)aFlag
{
_endView = aFlag;
}
 
- (BOOL)willRemoveFromSuperviewOnEnd
{
return _endView;
}
 
- (void)setCurrentProgress:(float)progress
{
[super setCurrentProgress:progress];
 
var progress = [self currentValue];
 
var keys = [properties allKeys],
count = [keys count];
 
for (var i = 0; i < count; i++)
{
var keyPath = keys[i],
property = [properties objectForKey:keyPath];
 
if (!property)
continue;
 
var start = property.start,
end = property.end,
value;
 
if (keyPath == 'width' || keyPath == 'height')
value = (progress * (end - start)) + start;
else if (keyPath == 'size')
value = CGSizeMake((progress * (end.width - start.width)) + start.width, (progress * (end.height - start.height)) + start.height);
else if (keyPath == 'frame')
{
value = CGRectMake(
(progress * (end.origin.x - start.origin.x)) + start.origin.x,
(progress * (end.origin.y - start.origin.y)) + start.origin.y,
(progress * (end.size.width - start.size.width)) + start.size.width,
(progress * (end.size.height - start.size.height)) + start.size.height);
}
else if (keyPath == 'alphaValue')
value = (progress * (end - start)) + start;
 
[view setValue:value forKey:keyPath];
}
}
 
- (void)startAnimation
{
var count = [properties count];
for (var i = 0; i < count; i++)
{
var keyPath = [properties allKeys][i],
property = [properties objectForKey:keyPath];
 
if (!property)
continue;
 
[view setValue:property.start forKey:keyPath];
}
 
if (_startView)
[_startView addSubview:view];
 
[super startAnimation];
}
 
- (void)animationDidEnd:(CPAnimation)anAnimation
{
if (_endView)
[view removeFromSuperview];
}
 
@end