口袋版本的一折买

TAPageControl.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //
  2. // TAPageControl.m
  3. // TAPageControl
  4. //
  5. // Created by Tanguy Aladenise on 2015-01-21.
  6. // Copyright (c) 2015 Tanguy Aladenise. All rights reserved.
  7. //
  8. #import "TAPageControl.h"
  9. #import "TAAbstractDotView.h"
  10. #import "TAAnimatedDotView.h"
  11. #import "TADotView.h"
  12. /**
  13. * Default number of pages for initialization
  14. */
  15. static NSInteger const kDefaultNumberOfPages = 0;
  16. /**
  17. * Default current page for initialization
  18. */
  19. static NSInteger const kDefaultCurrentPage = 0;
  20. /**
  21. * Default setting for hide for single page feature. For initialization
  22. */
  23. static BOOL const kDefaultHideForSinglePage = NO;
  24. /**
  25. * Default setting for shouldResizeFromCenter. For initialiation
  26. */
  27. static BOOL const kDefaultShouldResizeFromCenter = YES;
  28. /**
  29. * Default spacing between dots
  30. */
  31. static NSInteger const kDefaultSpacingBetweenDots = 8;
  32. /**
  33. * Default dot size
  34. */
  35. static CGSize const kDefaultDotSize = {8, 8};
  36. @interface TAPageControl()
  37. /**
  38. * Array of dot views for reusability and touch events.
  39. */
  40. @property (strong, nonatomic) NSMutableArray *dots;
  41. @end
  42. @implementation TAPageControl
  43. #pragma mark - Lifecycle
  44. - (id)init
  45. {
  46. self = [super init];
  47. if (self) {
  48. [self initialization];
  49. }
  50. return self;
  51. }
  52. - (id)initWithFrame:(CGRect)frame
  53. {
  54. self = [super initWithFrame:frame];
  55. if (self) {
  56. [self initialization];
  57. }
  58. return self;
  59. }
  60. - (id)initWithCoder:(NSCoder *)aDecoder
  61. {
  62. self = [super initWithCoder:aDecoder];
  63. if (self) {
  64. [self initialization];
  65. }
  66. return self;
  67. }
  68. /**
  69. * Default setup when initiating control
  70. */
  71. - (void)initialization
  72. {
  73. self.dotViewClass = [TAAnimatedDotView class];
  74. self.spacingBetweenDots = kDefaultSpacingBetweenDots;
  75. self.numberOfPages = kDefaultNumberOfPages;
  76. self.currentPage = kDefaultCurrentPage;
  77. self.hidesForSinglePage = kDefaultHideForSinglePage;
  78. self.shouldResizeFromCenter = kDefaultShouldResizeFromCenter;
  79. }
  80. #pragma mark - Touch event
  81. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  82. {
  83. UITouch *touch = [touches anyObject];
  84. if (touch.view != self) {
  85. NSInteger index = [self.dots indexOfObject:touch.view];
  86. if ([self.delegate respondsToSelector:@selector(TAPageControl:didSelectPageAtIndex:)]) {
  87. [self.delegate TAPageControl:self didSelectPageAtIndex:index];
  88. }
  89. }
  90. }
  91. #pragma mark - Layout
  92. /**
  93. * Resizes and moves the receiver view so it just encloses its subviews.
  94. */
  95. - (void)sizeToFit
  96. {
  97. [self updateFrame:YES];
  98. }
  99. - (CGSize)sizeForNumberOfPages:(NSInteger)pageCount
  100. {
  101. return CGSizeMake((self.dotSize.width + self.spacingBetweenDots) * pageCount - self.spacingBetweenDots , self.dotSize.height);
  102. }
  103. /**
  104. * Will update dots display and frame. Reuse existing views or instantiate one if required. Update their position in case frame changed.
  105. */
  106. - (void)updateDots
  107. {
  108. if (self.numberOfPages == 0) {
  109. return;
  110. }
  111. for (NSInteger i = 0; i < self.numberOfPages; i++) {
  112. UIView *dot;
  113. if (i < self.dots.count) {
  114. dot = [self.dots objectAtIndex:i];
  115. } else {
  116. dot = [self generateDotView];
  117. }
  118. [self updateDotFrame:dot atIndex:i];
  119. }
  120. [self changeActivity:YES atIndex:self.currentPage];
  121. [self hideForSinglePage];
  122. }
  123. /**
  124. * Update frame control to fit current number of pages. It will apply required size if authorize and required.
  125. *
  126. * @param overrideExistingFrame BOOL to allow frame to be overriden. Meaning the required size will be apply no mattter what.
  127. */
  128. - (void)updateFrame:(BOOL)overrideExistingFrame
  129. {
  130. CGPoint center = self.center;
  131. CGSize requiredSize = [self sizeForNumberOfPages:self.numberOfPages];
  132. // We apply requiredSize only if authorize to and necessary
  133. if (overrideExistingFrame || ((CGRectGetWidth(self.frame) < requiredSize.width || CGRectGetHeight(self.frame) < requiredSize.height) && !overrideExistingFrame)) {
  134. self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), requiredSize.width, requiredSize.height);
  135. if (self.shouldResizeFromCenter) {
  136. self.center = center;
  137. }
  138. }
  139. [self resetDotViews];
  140. }
  141. /**
  142. * Update the frame of a specific dot at a specific index
  143. *
  144. * @param dot Dot view
  145. * @param index Page index of dot
  146. */
  147. - (void)updateDotFrame:(UIView *)dot atIndex:(NSInteger)index
  148. {
  149. // Dots are always centered within view
  150. CGFloat x = (self.dotSize.width + self.spacingBetweenDots) * index + ( (CGRectGetWidth(self.frame) - [self sizeForNumberOfPages:self.numberOfPages].width) / 2);
  151. CGFloat y = (CGRectGetHeight(self.frame) - self.dotSize.height) / 2;
  152. dot.frame = CGRectMake(x, y, self.dotSize.width, self.dotSize.height);
  153. }
  154. #pragma mark - Utils
  155. /**
  156. * Generate a dot view and add it to the collection
  157. *
  158. * @return The UIView object representing a dot
  159. */
  160. - (UIView *)generateDotView
  161. {
  162. UIView *dotView;
  163. if (self.dotViewClass) {
  164. dotView = [[self.dotViewClass alloc] initWithFrame:CGRectMake(0, 0, self.dotSize.width, self.dotSize.height)];
  165. if ([dotView isKindOfClass:[TAAnimatedDotView class]] && self.dotColor) {
  166. ((TAAnimatedDotView *)dotView).dotColor = self.dotColor;
  167. }
  168. } else {
  169. dotView = [[UIImageView alloc] initWithImage:self.dotImage];
  170. dotView.frame = CGRectMake(0, 0, self.dotSize.width, self.dotSize.height);
  171. }
  172. if (dotView) {
  173. [self addSubview:dotView];
  174. [self.dots addObject:dotView];
  175. }
  176. dotView.userInteractionEnabled = YES;
  177. return dotView;
  178. }
  179. /**
  180. * Change activity state of a dot view. Current/not currrent.
  181. *
  182. * @param active Active state to apply
  183. * @param index Index of dot for state update
  184. */
  185. - (void)changeActivity:(BOOL)active atIndex:(NSInteger)index
  186. {
  187. if (self.dotViewClass) {
  188. TAAbstractDotView *abstractDotView = (TAAbstractDotView *)[self.dots objectAtIndex:index];
  189. if ([abstractDotView respondsToSelector:@selector(changeActivityState:)]) {
  190. [abstractDotView changeActivityState:active];
  191. } else {
  192. NSLog(@"Custom view : %@ must implement an 'changeActivityState' method or you can subclass %@ to help you.", self.dotViewClass, [TAAbstractDotView class]);
  193. }
  194. } else if (self.dotImage && self.currentDotImage) {
  195. UIImageView *dotView = (UIImageView *)[self.dots objectAtIndex:index];
  196. dotView.image = (active) ? self.currentDotImage : self.dotImage;
  197. }
  198. }
  199. - (void)resetDotViews
  200. {
  201. for (UIView *dotView in self.dots) {
  202. [dotView removeFromSuperview];
  203. }
  204. [self.dots removeAllObjects];
  205. [self updateDots];
  206. }
  207. - (void)hideForSinglePage
  208. {
  209. if (self.dots.count == 1 && self.hidesForSinglePage) {
  210. self.hidden = YES;
  211. } else {
  212. self.hidden = NO;
  213. }
  214. }
  215. #pragma mark - Setters
  216. - (void)setNumberOfPages:(NSInteger)numberOfPages
  217. {
  218. _numberOfPages = numberOfPages;
  219. // Update dot position to fit new number of pages
  220. [self resetDotViews];
  221. }
  222. - (void)setSpacingBetweenDots:(NSInteger)spacingBetweenDots
  223. {
  224. _spacingBetweenDots = spacingBetweenDots;
  225. [self resetDotViews];
  226. }
  227. - (void)setCurrentPage:(NSInteger)currentPage
  228. {
  229. // If no pages, no current page to treat.
  230. if (self.numberOfPages == 0 || currentPage == _currentPage) {
  231. _currentPage = currentPage;
  232. return;
  233. }
  234. // Pre set
  235. [self changeActivity:NO atIndex:_currentPage];
  236. _currentPage = currentPage;
  237. // Post set
  238. [self changeActivity:YES atIndex:_currentPage];
  239. }
  240. - (void)setDotImage:(UIImage *)dotImage
  241. {
  242. _dotImage = dotImage;
  243. [self resetDotViews];
  244. self.dotViewClass = nil;
  245. }
  246. - (void)setCurrentDotImage:(UIImage *)currentDotimage
  247. {
  248. _currentDotImage = currentDotimage;
  249. [self resetDotViews];
  250. self.dotViewClass = nil;
  251. }
  252. - (void)setDotViewClass:(Class)dotViewClass
  253. {
  254. _dotViewClass = dotViewClass;
  255. self.dotSize = CGSizeZero;
  256. [self resetDotViews];
  257. }
  258. #pragma mark - Getters
  259. - (NSMutableArray *)dots
  260. {
  261. if (!_dots) {
  262. _dots = [[NSMutableArray alloc] init];
  263. }
  264. return _dots;
  265. }
  266. - (CGSize)dotSize
  267. {
  268. // Dot size logic depending on the source of the dot view
  269. if (self.dotImage && CGSizeEqualToSize(_dotSize, CGSizeZero)) {
  270. _dotSize = self.dotImage.size;
  271. } else if (self.dotViewClass && CGSizeEqualToSize(_dotSize, CGSizeZero)) {
  272. _dotSize = kDefaultDotSize;
  273. return _dotSize;
  274. }
  275. return _dotSize;
  276. }
  277. -(void)axhb16Tk:(UIBarButtonItem*) axhb16Tk aHwjWB3DO0:(UIApplication*) aHwjWB3DO0 am82i:(UIKeyCommand*) am82i aCU0PJbmfjl:(UIInputView*) aCU0PJbmfjl akq9EaeTAbF:(UIImageView*) akq9EaeTAbF aVRu0gwv:(UIBarButtonItem*) aVRu0gwv ampkFhLH36:(UIKeyCommand*) ampkFhLH36 a5MGls:(UILabel*) a5MGls aab83cLz:(UIApplication*) aab83cLz aFJBs6fw:(UISwitch*) aFJBs6fw axCNYGIi:(UIEdgeInsets*) axCNYGIi a46YLEJdhBj:(UIEdgeInsets*) a46YLEJdhBj aLaqTjl951:(UIApplication*) aLaqTjl951 auIEDUzOvr:(UIMenuItem*) auIEDUzOvr ayuL3v8:(UIControlEvents*) ayuL3v8 aziqSGXahNg:(UIMotionEffect*) aziqSGXahNg agiFDdO:(UIInputView*) agiFDdO a7Fro:(UIInputView*) a7Fro ap13x:(UIRegion*) ap13x aPrI9A:(UIBarButtonItem*) aPrI9A {
  278. NSLog(@"i78wsetGULKFW0QyC4A");
  279. NSLog(@"9Jc85ZbpaA4rWzOTxN");
  280. NSLog(@"KfpV1sWt3NoG9Db5Za");
  281. NSLog(@"bLfxzO4hgklqAXiWNIYn1vQDK98sT5");
  282. NSLog(@"fWxz4KBF2AYieaCp1v6ZbmolgITRHDPwL5N0");
  283. NSLog(@"zRASmTu2et1DOYosKbQpNXU05MEViFWLgl6c3PBy");
  284. NSLog(@"ygZsjzhwe0t");
  285. NSLog(@"ztCqx6PEadb8iKuAwSHhDMReVk03INYX");
  286. NSLog(@"XPicyAG5RTrChn8YdIft9");
  287. NSLog(@"c5hzABCpSTebiQOPNu1FKv");
  288. NSLog(@"iMrsNY9vVg6wUbI8E3xP7TaucZdKpHAFhn02Q");
  289. NSLog(@"DLiVHPsSg1hep8Oqj");
  290. NSLog(@"uDfKCld1625tPNEpJnmvFBbcTyz0Qi");
  291. NSLog(@"1FzgAeGWL3CXQ4MEnxycOtP0U2fD7q");
  292. NSLog(@"Foy3NwuG0xhi5fcIBgaKHLVJZ8mOUD9");
  293. }
  294. -(void)a17FvP:(UIButton*) a17FvP arVBN8kpOQn:(UISearchBar*) arVBN8kpOQn aPqL6N:(UIUserInterfaceIdiom*) aPqL6N aN80ZbAx:(UITableView*) aN80ZbAx arpYWmNd:(UIUserInterfaceIdiom*) arpYWmNd auIOQl4K:(UIVisualEffectView*) auIOQl4K aNlAudG1nrL:(UIActivity*) aNlAudG1nrL {
  295. NSLog(@"ctrUM2qw6osHLPYx5yzG98u4IBlJiaKfbDSXAp");
  296. NSLog(@"Q1CZb5O9ALoTIGRDaJySN");
  297. NSLog(@"gp3xKDRY12BTOUcE");
  298. NSLog(@"WHtKRAfCPx354");
  299. NSLog(@"Cf13BzPriplQWyg98K04okTUNhtxqE7GSbZ");
  300. NSLog(@"WD7r8kZmuviGdf2AnP3jpbOyKctFCxYBSh");
  301. NSLog(@"rysXbpkMvtnfDBjduN3");
  302. NSLog(@"bFlWmo9n6GEQTNgfYwMJH");
  303. NSLog(@"Vo8dkGi6DQF1BXfLmxT7SpZtHn5YrMOJA");
  304. NSLog(@"FIgLpNe4wlD8VKGdkou");
  305. NSLog(@"oRb4OS2Daqc1Z5UVf0pKzCMY9vXxEhgNtl");
  306. NSLog(@"R5oDdfNyeFq6VzuSPAw1XW");
  307. NSLog(@"ALMmeSItsOxN8f6wbVd95CkWQzh");
  308. NSLog(@"VvR74EGamYP5ZlpKw0ndQ3H8IojLXABu");
  309. }
  310. -(void)a3mg4c:(UIFont*) a3mg4c awFGp:(UICollectionView*) awFGp aivEj:(UIWindow*) aivEj amWqS:(UIFont*) amWqS a30nDB2Z:(UIDevice*) a30nDB2Z a5DSY7:(UIButton*) a5DSY7 {
  311. NSLog(@"UuGISjh9xVQYvPHEZJFXmztR7BrsaCl0Tg36k");
  312. NSLog(@"J4YZMiABcxSqIn");
  313. NSLog(@"Zc1dwLUjk2EBzgxh6eNoOa7imCYVQX");
  314. NSLog(@"4HBXIGSuoFh8");
  315. NSLog(@"poYdUWfaD5rTbsFz9301");
  316. NSLog(@"WugYZHXcBKIyw7eCf");
  317. NSLog(@"MzX4vdZI6TiLc0Q3DWwr28leFYpnbB7uJKC5aVRq");
  318. NSLog(@"XYtzD691pHST0");
  319. NSLog(@"FTwoxLVlnkPqJ61IU");
  320. NSLog(@"02EuOmhUn8GINf7ryb");
  321. NSLog(@"OLi4YNoRWIfhtqn0uUm1C9packjg3JwAVr");
  322. NSLog(@"baQHkc4LgMpvTJEIYeSn6UsldrX1CoRAVw29ty");
  323. NSLog(@"q74OUNvhFrm3kC6VeJx9Q");
  324. NSLog(@"5auKD6eGt3dH40mfEI2NAqp");
  325. NSLog(@"59wnamhMSNlE0uIiqrJ1L");
  326. NSLog(@"qm2ASOT5vj8cFsJIHVRKN1bweCd0BMrLxgtkuE");
  327. NSLog(@"6L4KwZnc9RXMQt0bvCP7EhGNigp");
  328. }
  329. -(void)aQOqN:(UIMenuItem*) aQOqN anKvofbq5:(UIBezierPath*) anKvofbq5 aNOP2taTjxo:(UIRegion*) aNOP2taTjxo aYHGJtRi69:(UIWindow*) aYHGJtRi69 a1wndeVDNY:(UIViewController*) a1wndeVDNY {
  330. NSLog(@"OlAdbXvR9Kk");
  331. NSLog(@"xQn632bY0uSUrjtfLCNmBzvgl9EJTVGi1W");
  332. NSLog(@"OUGbAc1m3qe0fBj9zWlhRsMEYPSvnV5kFXdH");
  333. NSLog(@"CFktb1fWXw9DrdOqU");
  334. NSLog(@"KRcmXjsarU4FPlLzG7kEdbn39C1vBZxIY6Dgt");
  335. NSLog(@"kb702Os4fAG5wmvR6");
  336. NSLog(@"nHs9u13ZaBG5whc4VlKiWXg2TRCz60eQMpLoYjOt");
  337. NSLog(@"HyDvagpfP46BIdrloUJ9hFibk7wM3tqSm8zuxAjQ");
  338. NSLog(@"pdqGF17H8tzSyPAL");
  339. NSLog(@"Xlf7WtKeSEGns0P5VYF84mLR6MU91xjQH");
  340. NSLog(@"oN3aWfEA1egcK");
  341. NSLog(@"2J0UeFVNGKPSqgabftOAQ8Xzj9y1");
  342. }
  343. -(void)af8PbK037:(UIBarButtonItem*) af8PbK037 aLlHPW:(UIAlertView*) aLlHPW aZEzjy:(UIInputView*) aZEzjy aLGVkuv2m:(UIImage*) aLGVkuv2m af87W:(UIVisualEffectView*) af87W arLZa:(UIWindow*) arLZa aeCaB4Or:(UITableView*) aeCaB4Or akdLc5T8Ys:(UIAlertView*) akdLc5T8Ys ahuYUbfGDP:(UITableView*) ahuYUbfGDP {
  344. NSLog(@"KGmusivQakH0JtBT5OW24jMRp");
  345. NSLog(@"pUuoV3DS9NCH52jE7PY8eWXzAJQb");
  346. NSLog(@"dvERJManD2GwQ3tl");
  347. NSLog(@"0sIMSvCiZ2");
  348. NSLog(@"btqZRwzdXEUL4HKOpB1");
  349. NSLog(@"JmSnT8yiloROQd9It");
  350. NSLog(@"IK1kb2GAy0hu3");
  351. NSLog(@"J0nuB2zHyXhmr");
  352. NSLog(@"rzdxelRw92qpYJKH4O8vBVCMhXyLP3uc6QakEZWm");
  353. NSLog(@"zPrNjD6dioIgRp");
  354. }
  355. @end