-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSGFocusImageFrame.m
More file actions
217 lines (185 loc) · 7.61 KB
/
SGFocusImageFrame.m
File metadata and controls
217 lines (185 loc) · 7.61 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
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
//
// SGFocusImageFrame.m
// SGFocusImageFrame
//
// Created by Shane Gao on 17/6/12.
// Copyright (c) 2012 Shane Gao. All rights reserved.
//
#import "SGFocusImageFrame.h"
#import <objc/runtime.h>
#import "EGOImageView.h"
#pragma mark - SGFocusImageItem Definition
@implementation SGFocusImageItem
- (id)initWithTitle:(NSString *)title image:(NSString *)image tag:(NSInteger)tag
{
self = [super init];
if (self) {
self.title = title;
self.image = image;
self.tag = tag;
}
return self;
}
+ (id)itemWithTitle:(NSString *)title image:(NSString *)image tag:(NSInteger)tag
{
return [[SGFocusImageItem alloc] initWithTitle:title image:image tag:tag];
}
@end
#pragma mark - SGFocusImageFrame Definition
@interface SGFocusImageFrame ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
- (void)setupViews;
- (void)switchFocusImageItems;
@end
static NSString *SG_FOCUS_ITEM_ASS_KEY = @"com.touchmob.sgfocusitems";
static CGFloat SWITCH_FOCUS_PICTURE_INTERVAL = 3.0; //switch interval time
@implementation SGFocusImageFrame
- (id)initWithFrame:(CGRect)frame delegate:(id<SGFocusImageFrameDelegate>)delegate focusImageItemsArrray:(NSArray *)items
{
self = [super initWithFrame:frame];
if (self) {
objc_setAssociatedObject(self, (__bridge const void *)SG_FOCUS_ITEM_ASS_KEY, items, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
self.delegate = delegate;
[self initImageFrame];
}
return self;
}
- (id)initWithFrame:(CGRect)frame delegate:(id<SGFocusImageFrameDelegate>)delegate focusImageItems:(SGFocusImageItem *)firstItem, ...
{
self = [super initWithFrame:frame];
if (self) {
NSMutableArray *imageItems = [NSMutableArray array];
SGFocusImageItem *eachItem;
va_list argumentList;
if (firstItem)
{
[imageItems addObject: firstItem];
va_start(argumentList, firstItem);
while((eachItem = va_arg(argumentList, SGFocusImageItem *)))
{
[imageItems addObject: eachItem];
}
va_end(argumentList);
}
objc_setAssociatedObject(self, (__bridge const void *)SG_FOCUS_ITEM_ASS_KEY, imageItems, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
self.delegate = delegate;
[self initImageFrame];
}
return self;
}
- (void)dealloc
{
objc_setAssociatedObject(self, (__bridge const void *)SG_FOCUS_ITEM_ASS_KEY, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)initImageFrame
{
[self initParameters];
[self setupViews];
}
#pragma mark - private methods
- (void)initParameters
{
self.switchTimeInterval = SWITCH_FOCUS_PICTURE_INTERVAL;
self.autoScrolling = YES;
}
- (void)setupViews
{
NSArray *imageItems = objc_getAssociatedObject(self, (__bridge const void *)SG_FOCUS_ITEM_ASS_KEY);
CGFloat mainWidth = self.frame.size.width, mainHeight = self.frame.size.height;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.f, 0.f, mainWidth, mainHeight)];
CGSize size = CGSizeMake(mainWidth, 20);
CGRect pcFrame = CGRectMake(mainWidth *.5 - size.width *.5, mainHeight - size.height, size.width, size.height);
UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:pcFrame];
[pageControl addTarget:self action:@selector(pageControlTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:scrollView];
[self addSubview:pageControl];
/*
scrollView.layer.cornerRadius = 10;
scrollView.layer.borderWidth = 1 ;
scrollView.layer.borderColor = [[UIColor lightGrayColor ] CGColor];
*/
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
pageControl.numberOfPages = imageItems.count;
pageControl.currentPage = 0;
scrollView.delegate = self;
// [scrollView setShouldGroupAccessibilityChildren:YES];
// single tap gesture recognizer
UITapGestureRecognizer *tapGestureRecognize = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(singleTapGestureRecognizer:)];
tapGestureRecognize.delegate = self;
tapGestureRecognize.numberOfTapsRequired = 1;
[scrollView addGestureRecognizer:tapGestureRecognize];
CGSize scrollViewSize = scrollView.frame.size;
scrollView.contentSize = CGSizeMake(scrollViewSize.width * imageItems.count, scrollViewSize.height);
for (int i = 0; i < imageItems.count; i++) {
SGFocusImageItem *item = [imageItems objectAtIndex:i];
EGOImageView *imageView = [[EGOImageView alloc] initWithFrame:CGRectMake(i * scrollViewSize.width, 0, scrollViewSize.width, scrollViewSize.height)];
// NSLog(@"=============%f",imageView.frame.origin.x);
NSLog(@"=============%f",scrollViewSize.height);
imageView.imageURL = [NSURL URLWithString:item.image];
[scrollView addSubview:imageView];
}
self.scrollView = scrollView;
self.pageControl = pageControl;
}
#pragma mark - Actions
- (IBAction)pageControlTapped:(id)sender
{
UIPageControl *pc = (UIPageControl *)sender;
[self moveToTargetPage:pc.currentPage];
}
- (void)singleTapGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
int targetPage = (int)(self.scrollView.contentOffset.x / self.scrollView.frame.size.width);
NSArray *imageItems = objc_getAssociatedObject(self, (__bridge const void *)SG_FOCUS_ITEM_ASS_KEY);
if (targetPage > -1 && targetPage < imageItems.count) {
SGFocusImageItem *item = [imageItems objectAtIndex:targetPage];
if ([self.delegate respondsToSelector:@selector(foucusImageFrame:didSelectItem:)]) {
[self.delegate foucusImageFrame:self didSelectItem:item];
}
}
}
#pragma mark - ScrollView MOve
- (void)moveToTargetPage:(NSInteger)targetPage
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(switchFocusImageItems) object:nil];
CGFloat targetX = targetPage * self.scrollView.frame.size.width;
[self moveToTargetPosition:targetX];
[self performSelector:@selector(switchFocusImageItems) withObject:nil afterDelay:self.switchTimeInterval];
}
- (void)switchFocusImageItems
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(switchFocusImageItems) object:nil];
CGFloat targetX = self.scrollView.contentOffset.x + self.scrollView.frame.size.width;
[self moveToTargetPosition:targetX];
if (self.autoScrolling) {
[self performSelector:@selector(switchFocusImageItems) withObject:nil afterDelay:self.switchTimeInterval];
}
}
- (void)moveToTargetPosition:(CGFloat)targetX
{
//NSLog(@"moveToTargetPosition : %f" , targetX);
if (targetX >= self.scrollView.contentSize.width) {
targetX = 0.0;
}
[self.scrollView setContentOffset:CGPointMake(targetX, 0) animated:YES] ;
self.pageControl.currentPage = (int)(self.scrollView.contentOffset.x / self.scrollView.frame.size.width);
}
- (void)setAutoScrolling:(BOOL)enable
{
_autoScrolling = enable;
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(switchFocusImageItems) object:nil];
if (_autoScrolling) {
[self performSelector:@selector(switchFocusImageItems) withObject:nil afterDelay:self.switchTimeInterval];
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);
}
@end