forked from ArchipelProject/TNKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TNSwipeView.j
508 lines (429 loc) · 15.2 KB
/
TNSwipeView.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* TNSwipeView.j
*
* Copyright (C) 2010 Antoine Mercadal <[email protected]>
*
* 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 3.0 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
*/
@import <Foundation/Foundation.j>
@import <AppKit/CPControl.j>
@import <AppKit/CPView.j>
var CSSProperties = {
"webkit" : {
"transform": "WebkitTransform",
"backfaceVisibility": "WebkitBackfaceVisibility",
"perspective": "WebkitPerspective",
"transformStyle": "WebkitTransformStyle",
"transition": "WebkitTransition",
"transitionTimingFunction": "WebkitTransitionTimingFunction",
"transitionEnd": "webkitTransitionEnd"
},
"gecko" : {
"transform": "MozTransform",
"backfaceVisibility": "MozBackfaceVisibility",
"perspective": "MozPerspective",
"transformStyle": "MozTransformStyle",
"transition": "MozTransition",
"transitionTimingFunction": "MozTransitionTimingFunction",
"transitionEnd": "transitionend"
}
};
TNSwipeViewDirectionRight = 1;
TNSwipeViewDirectionLeft = 2;
TNSwipeViewCSSTranslateFunctionX = @"translateX";
TNSwipeViewCSSTranslateFunctionY = @"translateY";
// handle flatten & press
try
{
TNSwipeViewBrowserEngine = (typeof(document.body.style.WebkitTransform) != "undefined") ? "webkit" : "gecko";
}
catch(e)
{
TNSwipeViewBrowserEngine = "gecko";
}
/*! @ingroup TNKit
This widget allows to add custom views in it and swipe them as pages
Note that is use CSS transformation
*/
@implementation TNSwipeView : CPControl
{
CPArray _views @accessors(getter=views);
CPString _translationFunction @accessors(getter=translationFunction);
float _animationDuration @accessors(property=animationDuration);
float _minimalRatio @accessors(property=minimalRatio);
BOOL _isAnimating;
CGPoint _generalInitialTrackingPoint;
CGPoint _initialTrackingPoint;
CPView _mainView;
Function _validateFunction;
CPTimer _animationGuardTimer;
int _currentViewIndex;
}
#pragma mark -
#pragma mark Initialization
/*! initialize the TNFlipView
@param aRect the frame
*/
- (TNSwipeView)initWithFrame:(CGRect)aRect
{
if (self = [super initWithFrame:aRect])
{
_animationDuration = 0.3;
_currentViewIndex = 0;
_minimalRatio = 0.3;
_isAnimating = NO;
_mainView = [[CPView alloc] initWithFrame:[self bounds]];
_translationFunction = TNSwipeViewCSSTranslateFunctionX;
_views = [CPArray array];
_validateFunction = function(e){
this.removeEventListener(CSSProperties[TNSwipeViewBrowserEngine].transitionEnd, _validateFunction, YES);
_mainView._DOMElement.style[CSSProperties[TNSwipeViewBrowserEngine].transition] = "0s";
[self _commitAnimation];
};
[_mainView setAutoresizingMask:CPViewHeightSizable];
[self addSubview:_mainView];
[self setNeedsLayout];
}
return self;
}
/*! initialize the TNSwipeView
@param aRect the frame
@param aFunction the CSS transformation to use
*/
- (TNSwipeView)initWithFrame:(CGRect)aRect translationFunction:(CPString)aFunction
{
if (self = [self initWithFrame:aRect])
{
_translationFunction = aFunction;
}
return self
}
#pragma mark -
#pragma mark Notification handlers
- (void)resetAnimating:(CPTimer)aTimer
{
_isAnimating = NO;
_animationGuardTimer = nil;
}
#pragma mark -
#pragma mark Getters / Setters
/*! set the content of the TNSwipeView
@param someViews CPArray containing views
*/
- (void)setViews:(CPArray)someViews
{
_views = someViews;
[self setNeedsLayout];
}
#pragma mark -
#pragma mark Overrides
- (void)mouseDown:(CPEvent)anEvent
{
_initialTrackingPoint = [_mainView convertPointFromBase:[anEvent globalLocation]];
_generalInitialTrackingPoint = [self convertPointFromBase:[anEvent globalLocation]];
[super mouseDown:anEvent];
}
- (void)trackMouse:(CPEvent)anEvent
{
if (!_isAnimating)
{
var currentDraggingPoint = [_mainView convertPointFromBase:[anEvent globalLocation]];
if (_translationFunction == TNSwipeViewCSSTranslateFunctionX)
{
currentDraggingPoint.x -= _initialTrackingPoint.x;
[self _setSlideValue:currentDraggingPoint.x speed:0.05 shouldCommit:NO];
}
else
{
currentDraggingPoint.y -= _initialTrackingPoint.y;
[self _setSlideValue:currentDraggingPoint.y speed:0.05 shouldCommit:NO];
}
}
else
{
CPLog.warn("Animating.... forget it");
}
[super trackMouse:anEvent];
}
- (void)stopTracking:(CGPoint)lastPoint at:(CGPoint)aPoint mouseIsUp:(BOOL)mouseIsUp
{
[super stopTracking:lastPoint at:aPoint mouseIsUp:mouseIsUp];
if (!mouseIsUp)
return;
if (_isAnimating)
return;
var movement,
minimalMovement;
if (_translationFunction == TNSwipeViewCSSTranslateFunctionX)
{
movement = _generalInitialTrackingPoint.x - aPoint.x;
minimalMovement = [self frameSize].width * _minimalRatio;
}
else
{
movement = _generalInitialTrackingPoint.y - aPoint.y;
minimalMovement = [self frameSize].height * _minimalRatio;
}
if (movement != 0 && Math.abs(movement) >= minimalMovement)
{
if (movement < 0)
{
if (_currentViewIndex > 0)
{
[self _performDirectionalSlide:TNSwipeViewDirectionRight];
return;
}
}
else
{
if (_currentViewIndex < [_views count] - 1)
{
[self _performDirectionalSlide:TNSwipeViewDirectionLeft];
return;
}
}
}
[self _resetTranslation];
}
- (void)layoutSubviews
{
if (_translationFunction == TNSwipeViewCSSTranslateFunctionX)
{
[_mainView setFrameSize:CGSizeMake([self frameSize].width * [_views count], [self frameSize].height)];
for (var i = 0, c = [_views count]; i < c; i++)
{
var currentView = _views[i];
[currentView setFrame:[self bounds]];
[currentView setFrameOrigin:CGPointMake(i * [self frameSize].width, 0)];
[_mainView addSubview:currentView];
}
}
else
{
[_mainView setFrameSize:CGSizeMake([self frameSize].width, [self frameSize].height * [_views count])];
for (var i = 0, c = [_views count]; i < c; i++)
{
var currentView = _views[i];
[currentView setFrame:[self bounds]];
[currentView setFrameOrigin:CGPointMake(0, i * [self frameSize].height)];
[_mainView addSubview:currentView];
}
}
}
- (void)setFrame:(CGRect)aFrame
{
var currentFrameWidth = [self frameSize].width,
widthOffset;
[super setFrame:aFrame];
if (_currentViewIndex == 0)
return;
widthOffset = aFrame.size.width - currentFrameWidth;
[_mainView setFrameOrigin:CGPointMake(([_mainView frameOrigin].x - (widthOffset * _currentViewIndex)) , 0)];
}
#pragma mark -
#pragma mark Utilities
/*! @ignore
*/
- (void)_setSlideValue:(float)aDirectionalSlideValue speed:(float)aSpeed shouldCommit:(BOOL)shouldCommit
{
_mainView._DOMElement.style[CSSProperties[TNSwipeViewBrowserEngine].transition] = aSpeed + @"s";
if (shouldCommit)
{
// yeah yeah, we'll try to fix this later...
_animationGuardTimer = [CPTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(resetAnimating:)
userInfo:nil
repeats:NO];
_isAnimating = YES;
_mainView._DOMElement.addEventListener(CSSProperties[TNSwipeViewBrowserEngine].transitionEnd, _validateFunction, YES);
}
_mainView._DOMElement.style[CSSProperties[TNSwipeViewBrowserEngine].backfaceVisibility] = "hidden";
_mainView._DOMElement.style[CSSProperties[TNSwipeViewBrowserEngine].perspective] = 1000;
_mainView._DOMElement.style[CSSProperties[TNSwipeViewBrowserEngine].transformStyle] = "preserve-3d";
_mainView._DOMElement.style[CSSProperties[TNSwipeViewBrowserEngine].transform] = _translationFunction + @"(" + aDirectionalSlideValue + @"px)";
}
/*! _performDirectionalSlide directly to the current view index. If anIndex is
greater than the actual number of views, last view will be selected
if anIndex is lesser than 0, then the first view will be selected
@param anIndex the index of the view to display
*/
- (void)slideToViewIndex:(int)anIndex
{
if (anIndex == _currentViewIndex)
return;
if (anIndex > [_views count] - 1)
anIndex == [_views count] - 1;
if (anIndex < 0)
anIndex = 0;
if (anIndex > _currentViewIndex)
{
if (_translationFunction == TNSwipeViewCSSTranslateFunctionX)
[self _setSlideValue:- (anIndex - _currentViewIndex) * [self frameSize].width speed:_animationDuration shouldCommit:YES];
else
[self _setSlideValue:- (anIndex - _currentViewIndex) * [self frameSize].height speed:_animationDuration shouldCommit:YES];
}
else if (anIndex < _currentViewIndex)
{
if (_translationFunction == TNSwipeViewCSSTranslateFunctionX)
[self _setSlideValue:(_currentViewIndex - anIndex) * [self frameSize].width speed:_animationDuration shouldCommit:YES];
else
[self _setSlideValue:(_currentViewIndex - anIndex) * [self frameSize].height speed:_animationDuration shouldCommit:YES];
}
_currentViewIndex = anIndex;
}
/*! @ignore
reset the eventual not commited translation
*/
- (void)_resetTranslation
{
[self _setSlideValue:0.0 speed:_animationDuration shouldCommit:YES];
}
/*! @ignore
perform a _performDirectionalSlide by 1 in given direction.
@param the direction TNSwipeViewDirectionRight, or TNSwipeViewDirectionLeft
*/
- (void)_performDirectionalSlide:(int)aDirection
{
if (_isAnimating)
return;
var offset;
switch (aDirection)
{
case TNSwipeViewDirectionLeft:
if (_currentViewIndex + 1 < [_views count])
{
_currentViewIndex++;
if (_translationFunction == TNSwipeViewCSSTranslateFunctionX)
offset = - [self frameSize].width;
else
offset = - [self frameSize].height;
}
else
{
_currentViewIndex = 0;
if (_translationFunction == TNSwipeViewCSSTranslateFunctionX)
offset = [self frameSize].width * ([_views count] - 1);
else
offset = [self frameSize].height * ([_views count] - 1);
}
break;
case TNSwipeViewDirectionRight:
if (_currentViewIndex > 0)
{
_currentViewIndex--;
if (_translationFunction == TNSwipeViewCSSTranslateFunctionX)
offset = [self frameSize].width;
else
offset = [self frameSize].height;
}
else
{
_currentViewIndex = [_views count] - 1;
if (_translationFunction == TNSwipeViewCSSTranslateFunctionX)
offset = - (_currentViewIndex * [self frameSize].width);
else
offset = - (_currentViewIndex * [self frameSize].height);
}
break;
}
[self _setSlideValue:offset speed:_animationDuration shouldCommit:YES];
}
/*! @ignore
returns the current translation offset
@return integer representing the pixel offset
*/
- (int)_currentTranslation
{
var t = _mainView._DOMElement.style[CSSProperties[TNSwipeViewBrowserEngine].transform];
t = t.replace(_translationFunction + @"(", @"");
t = t.replace(@"px)", @"");
if (t == "")
t = 0;
return parseInt(t);
}
/*! @ignore
commit the current CSS translation by moving the actual
position of the main view and be reseting the translation offset
*/
- (void)_commitAnimation
{
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
if (_translationFunction == TNSwipeViewCSSTranslateFunctionX)
{
var tx = [self _currentTranslation],
newX = [_mainView frameOrigin].x + tx;
[_mainView setFrameOrigin:CGPointMake(newX, 0)];
}
else
{
var ty = [self _currentTranslation],
newY = [_mainView frameOrigin].y + ty;
[_mainView setFrameOrigin:CGPointMake(0, newY)];
}
_mainView._DOMElement.style[CSSProperties[TNSwipeViewBrowserEngine].transform] = _translationFunction + @"(0px)";
if (_animationGuardTimer)
[_animationGuardTimer invalidate];
[_mainView setNeedsDisplay:YES];
_animationGuardTimer = nil;
_isAnimating = NO;
}
#pragma mark -
#pragma mark Actions
/*! select the next view.
if current view is the last one, first view will be selected
@param aSender the sender of the action
*/
- (IBAction)nextView:(id)aSender
{
[self _performDirectionalSlide:TNSwipeViewDirectionLeft];
}
/*! select the previous view.
if current view is the first one, last view will be selected
@param aSender the sender of the action
*/
- (IBAction)previousView:(id)aSender
{
[self _performDirectionalSlide:TNSwipeViewDirectionRight];
}
@end
@implementation TNSwipeView (CPCoding)
/*! CPCoder compliance
*/
- (id)initWithCoder:(CPCoder)aCoder
{
self = [super initWithCoder:aCoder];
if (self)
{
_views = [aCoder decodeObjectForKey:@"_views"];
_translationFunction = [aCoder decodeObjectForKey:@"_translationFunction"];
_animationDuration = [aCoder decodeObjectForKey:@"_animationDuration"];
_mainView = [aCoder decodeObjectForKey:@"_mainView"];
_minimalRatio = [aCoder decodeObjectForKey:@"_minimalRatio"];
}
return self;
}
/*! CPCoder compliance
*/
- (void)encodeWithCoder:(CPCoder)aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeObject:_views forKey:@"_views"];
[aCoder encodeObject:_translationFunction forKey:@"_translationFunction"];
[aCoder encodeObject:_animationDuration forKey:@"_animationDuration"];
[aCoder encodeObject:_mainView forKey:@"_mainView"];
[aCoder encodeObject:_minimalRatio forKey:@"_minimalRatio"];
}
@end