API  1.0.0
CPViewAnimation.j
Go to the documentation of this file.
1 /*
2  * CPViewAnimation.j
3  * AppKit
4  *
5  * Created by Klaas Pieter Annema on September 3, 2009.
6  * Copyright 2009, Sofa BV
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
25 CPViewAnimationTargetKey = @"CPViewAnimationTargetKey";
26 CPViewAnimationStartFrameKey = @"CPViewAnimationStartFrameKey";
27 CPViewAnimationEndFrameKey = @"CPViewAnimationEndFrameKey";
28 CPViewAnimationEffectKey = @"CPViewAnimationEffectKey";
29 
30 CPViewAnimationFadeInEffect = @"CPViewAnimationFadeInEffect";
31 CPViewAnimationFadeOutEffect = @"CPViewAnimationFadeOutEffect";
32 
39 @implementation CPViewAnimation : CPAnimation
40 {
41  CPArray _viewAnimations;
42 }
43 
71 - (id)initWithViewAnimations:(CPArray)viewAnimations
72 {
73  if (self = [super initWithDuration:0.5 animationCurve:CPAnimationLinear])
74  {
75  [self setViewAnimations:viewAnimations];
76  }
77 
78  return self;
79 }
80 
81 - (void)startAnimation
82 {
83  var animationIndex = [_viewAnimations count];
84  while (animationIndex--)
85  {
86  var dictionary = [_viewAnimations objectAtIndex:animationIndex],
87  view = [self _targetView:dictionary],
88  startFrame = [self _startFrame:dictionary];
89 
90  [view setFrame:startFrame];
91 
92  var effect = [self _effect:dictionary];
93  if (effect === CPViewAnimationFadeInEffect)
94  {
95  [view setAlphaValue:0.0];
96  [self _targetView:view setHidden:NO];
97  }
98  else if (effect === CPViewAnimationFadeOutEffect)
99  [view setAlphaValue:1.0];
100  }
101 
102  [super startAnimation];
103 }
104 
105 - (void)setCurrentProgress:(float)progress
106 {
107  [super setCurrentProgress:progress];
108 
109  var animationIndex = [_viewAnimations count];
110  while (animationIndex--)
111  {
112  var dictionary = [_viewAnimations objectAtIndex:animationIndex],
113  view = [self _targetView:dictionary],
114  startFrame = [self _startFrame:dictionary],
115  endFrame = [self _endFrame:dictionary],
116  differenceFrame = CGRectMakeZero(),
117  value = [super currentValue];
118 
119  differenceFrame.origin.x = endFrame.origin.x - startFrame.origin.x;
120  differenceFrame.origin.y = endFrame.origin.y - startFrame.origin.y;
121  differenceFrame.size.width = endFrame.size.width - startFrame.size.width;
122  differenceFrame.size.height = endFrame.size.height - startFrame.size.height;
123 
124  var intermediateFrame = CGRectMakeZero();
125  intermediateFrame.origin.x = startFrame.origin.x + differenceFrame.origin.x * value;
126  intermediateFrame.origin.y = startFrame.origin.y + differenceFrame.origin.y * value;
127  intermediateFrame.size.width = startFrame.size.width + differenceFrame.size.width * value;
128  intermediateFrame.size.height = startFrame.size.height + differenceFrame.size.height * value;
129 
130  [view setFrame:intermediateFrame];
131 
132  // Update the view's alpha value
133  var effect = [self _effect:dictionary];
134  if (effect === CPViewAnimationFadeInEffect)
135  [view setAlphaValue:1.0 * value];
136  else if (effect === CPViewAnimationFadeOutEffect)
137  [view setAlphaValue:1.0 + ( 0.0 - 1.0 ) * value];
138 
139  if (progress === 1.0)
140  [self _targetView:view setHidden:CGRectIsEmpty(endFrame) || [view alphaValue] === 0.0];
141  }
142 }
143 
144 - (void)stopAnimation
145 {
146  var animationIndex = [_viewAnimations count];
147  while (animationIndex--)
148  {
149  var dictionary = [_viewAnimations objectAtIndex:animationIndex],
150  view = [self _targetView:dictionary],
151  endFrame = [self _endFrame:dictionary];
152 
153  [view setFrame:endFrame];
154 
155  var effect = [self _effect:dictionary];
156  if (effect === CPViewAnimationFadeInEffect)
157  [view setAlphaValue:1.0];
158  else if (effect === CPViewAnimationFadeOutEffect)
159  [view setAlphaValue:0.0];
160 
161  [self _targetView:view setHidden:CGRectIsEmpty(endFrame) || [view alphaValue] === 0.0];
162  }
163 
164  [super stopAnimation];
165 }
166 
167 - (void)_targetView:(id)theView setHidden:(BOOL)isHidden
168 {
169  if ([theView isKindOfClass:[CPWindow class]])
170  {
171  if (isHidden)
172  [theView orderOut:self];
173  else
174  [theView orderFront:self];
175  }
176  else
177  [theView setHidden:isHidden];
178 }
179 
180 - (id)_targetView:(CPDictionary)dictionary
181 {
182  var targetView = [dictionary valueForKey:CPViewAnimationTargetKey];
183  if (!targetView)
184  [CPException raise:CPInternalInconsistencyException reason:[CPString stringWithFormat:@"view animation: %@ does not have a target view", [dictionary description]]];
185 
186  return targetView;
187 }
188 
189 - (CGRect)_startFrame:(CPDictionary)dictionary
190 {
191  var startFrame = [dictionary valueForKey:CPViewAnimationStartFrameKey];
192  if (!startFrame)
193  return [[self _targetView:dictionary] frame];
194 
195  return startFrame;
196 }
197 
198 - (CGRect)_endFrame:(CPDictionary)dictionary
199 {
200  var endFrame = [dictionary valueForKey:CPViewAnimationEndFrameKey];
201  if (!endFrame)
202  return [[self _targetView:dictionary] frame];
203 
204  return endFrame;
205 }
206 
207 - (CPString)_effect:(CPDictionary)dictionary
208 {
209  return [dictionary valueForKey:CPViewAnimationEffectKey];
210 }
211 
212 - (CPArray)viewAnimations
213 {
214  return _viewAnimations;
215 }
216 
222 - (void)setViewAnimations:(CPArray)viewAnimations
223 {
224  if (viewAnimations != _viewAnimations)
225  {
226  [self stopAnimation];
227  _viewAnimations = [viewAnimations copy];
228  }
229 }
230 
231 @end
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
CPArray viewAnimations()
BOOL isHidden()
Definition: CALayer.j:597
CPViewAnimationStartFrameKey
CPViewAnimationFadeInEffect
CPString description()
Definition: CPDictionary.j:622
float currentValue()
Definition: CPAnimation.j:324
CPViewAnimationFadeOutEffect
CPViewAnimationEndFrameKey
CPViewAnimationTargetKey
void stopAnimation()
Definition: CPAnimation.j:284
void raise:reason:(CPString aName, [reason] CPString aReason)
Definition: CPException.j:66
A mutable key-value pair collection.
Definition: CPDictionary.h:2
void setViewAnimations:(CPArray viewAnimations)
An immutable string (collection of characters).
Definition: CPString.h:2
void startAnimation()
Definition: CPAnimation.j:241
CPAnimationLinear
Definition: CPAnimation.j:46
id valueForKey:(CPString aKey)
void setCurrentProgress:(float aProgress)
Definition: CPAnimation.j:308
CPViewAnimationEffectKey
id stringWithFormat:(CPString format, [,] ...)
Definition: CPString.j:166