-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathYCSlideShowImageView.m
144 lines (117 loc) · 3.32 KB
/
YCSlideShowImageView.m
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
//
// YCSlideShowView.m
// YU - YUPPIU
//
// Created by Fabio Knoedt on 11/08/14.
// Copyright (c) 2014 Datedicted. All rights reserved.
//
#import "YCSlideShowImageView.h"
@interface YCSlideShowImageView ()
@property (atomic, retain) NSMutableArray *images;
@end
@implementation YCSlideShowImageView
/*!
* @brief Init method for object.
* @return The initialized object.
*/
- (id)init;
{
if ((self = [super init]))
{
/// Initializes the default values.
[self initDefaultValues];
}
return self;
}
/*!
* @brief Init method for object.
* @return The initialized object.
*/
- (id)initWithCoder:(NSCoder *)aDecoder;
{
if ((self = [super initWithCoder:aDecoder]))
{
/// Initializes the default values.
[self initDefaultValues];
}
return self;
}
/*!
* @brief Init method for object.
* @return The initialized object.
*/
- (id)initWithFrame:(CGRect)frame;
{
if ((self = [super initWithFrame:frame]))
{
/// Initializes the default values.
[self initDefaultValues];
}
return self;
}
/*!
* @brief Initializes the default values.
*/
- (void)initDefaultValues;
{
/// Public property for animation duration.
_animationDurationTime = 1.0f;
/// Public property for animation delay.
_animationDelay = 2.5f;
/// Public property for animation options (transition effects).
_animationOptions = UIViewAnimationOptionTransitionCrossDissolve;
}
/*!
* @brief Animate an UIImageView with different images.
* @param image An UIImage to add to the current slide show.
*/
- (void)addImage:(UIImage *)image;
{
/// If the array is not initialized, we create it.
if (!_images) {
/// If it is not initialized.
_images = [NSMutableArray arrayWithObject:image];
} else {
/// Add the new image to the array.
[_images addObject:image];
}
/// If we have 2 images already, start the animation.
/// If not, we just the first image to the UIImageView without animation.
if ([_images count] == 2) {
/// Animate.
[self animateWithIndex:0];
} else {
/// Set the very first image.
[self setImage:image];
}
}
/*!
* @brief Animate an UIImageView with different images.
* @param index The index of the next image to display.
*/
- (void)animateWithIndex:(NSUInteger)index;
{
/// Reset index.
index = (index >= [_images count]) ? 0 : index;
/// Animate with CrossDissolve effect.
[UIView transitionWithView:self duration:_animationDurationTime options:_animationOptions animations:^{
/// Set the next image.
[self performSelectorOnMainThread:@selector(setImage:) withObject:_images[index] waitUntilDone:NO];
} completion:^(BOOL finished) {
/// Call after some delay.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, _animationDelay * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
/// If we still have images, we recursively call the method again.
if (_images && [_images count]) {
[self animateWithIndex:index+1];
}
});
}];
}
/*!
* @brief Empty the array with images, this will stop animating.
*/
- (void)emptyImageArray
{
_images = nil;
}
@end