DocumentPageViewController.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. //
  2. // DocumentPageViewController.m
  3. // AntsContract
  4. //
  5. // Created by Ray on 12/16/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "DocumentPageViewController.h"
  9. #import "PageViewController.h"
  10. #import "SignatureListViewController.h"
  11. #import "SignatureViewController.h"
  12. #import "PDFUtils.h"
  13. #import "ACNetwork.h"
  14. #import "AFHTTPSessionManager.h"
  15. #import "AppDelegate.h"
  16. #import "ACNetwork.h"
  17. #import "RAUtils.h"
  18. #import "PDFViewController.h"
  19. #import "config.h"
  20. #import "AESCrypt.h"
  21. #import "CustomIOSAlertView.h"
  22. #import "ImageUtils.h"
  23. @interface DocumentPageViewController ()
  24. @end
  25. @implementation DocumentPageViewController
  26. -(NSMutableDictionary*) templateInit:(NSMutableDictionary*) template
  27. {
  28. NSString* temp = NSTemporaryDirectory();
  29. NSString* filename =[NSString stringWithFormat:@"%@.png", [[NSUUID UUID] UUIDString]];
  30. NSString* qrpath=[temp stringByAppendingPathComponent:filename];
  31. UIImage* qrimg=[RAUtils generateBarCode:self.docment_id width:200 height:44];//[QRCodeGenerator qrImageForString:order_code imageSize:240];
  32. bool bwrite=[UIImagePNGRepresentation(qrimg)writeToFile: qrpath atomically:YES];
  33. for(int i=0;i<[template[@"count"] intValue];i++)
  34. {
  35. NSMutableDictionary* page =template[[NSString stringWithFormat:@"page_%d",i]];
  36. for(int j=0;j<[page[@"count"] intValue];j++)
  37. {
  38. NSMutableDictionary* control =page[[NSString stringWithFormat:@"control_%d",j]];
  39. NSString* type = control[@"type"];
  40. NSString* field =control[@"field"];
  41. if([type isEqualToString:@"Label"])
  42. {
  43. if([field isEqualToString:@"doc_number"])
  44. {
  45. control[@"value"]= self.docment_id;
  46. }
  47. }
  48. else if([type isEqualToString:@"Image"])
  49. {
  50. NSString* name =control[@"name"];
  51. if([name isEqualToString:@"BarCode"])
  52. {
  53. control[@"value"]= qrpath;
  54. }
  55. }
  56. }
  57. }
  58. return template;
  59. }
  60. - (void)viewDidLoad {
  61. [super viewDidLoad];
  62. // self.delegate = self;
  63. // self.dataSource = self;
  64. self.signatureData= [[NSMutableDictionary alloc] init];
  65. self.document = [PDFUtils OpenPDF:self.pdfName];
  66. self.total_page = CGPDFDocumentGetNumberOfPages (self.document);
  67. self.controlTemplate = [self templateInit:[PDFUtils loadControl:self.templateName]];
  68. PageViewController* startingViewController=[self viewControllerAtIndex:0];
  69. // NSArray* viewControllers =[NSArray arrayWithObjects:page_0,nil];
  70. self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
  71. self.pageViewController.delegate = self;
  72. // PageViewController *startingViewController = [self viewControllerAtIndex:0 storyboard:self.storyboard];
  73. NSArray *viewControllers = @[startingViewController];
  74. [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
  75. self.pageViewController.dataSource = self;
  76. [self addChildViewController:self.pageViewController];
  77. [self.view addSubview:self.pageViewController.view];
  78. // Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
  79. CGRect pageViewRect = self.view.bounds;
  80. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  81. pageViewRect = pageViewRect;//CGRectInset(pageViewRect, 40.0, 40.0);
  82. }
  83. self.pageViewController.view.frame = pageViewRect;
  84. [self.pageViewController didMoveToParentViewController:self];
  85. // Do any additional setup after loading the view.
  86. }
  87. -(void) dealloc
  88. {
  89. CFBridgingRelease(self.document);
  90. }
  91. - (void)didReceiveMemoryWarning {
  92. [super didReceiveMemoryWarning];
  93. // Dispose of any resources that can be recreated.
  94. }
  95. - (IBAction)onSignatureClicked:(id)sender {
  96. // CGRect cellrect_screen = [RAUtils relativeFrameForScreenWithView:trigger];
  97. //
  98. //
  99. // CGRect rect1=[self.view convertRect:cellrect_screen fromView:[[[UIApplication sharedApplication] delegate] window]];
  100. //
  101. // DebugLog(@"convert1 %@",NSStringFromCGRect(rect1));
  102. SignatureListViewController *signatureVC = [ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureListViewController"];
  103. signatureVC.title=@"Signatures";
  104. signatureVC.signatureData = self.signatureData;
  105. signatureVC.blk_Add = ^()
  106. {
  107. __block UIImage* signimg=nil;
  108. SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"];
  109. vc.onReturnImg = ^(UIImage* img)
  110. {
  111. signimg = img;
  112. if(signimg!=nil)
  113. [PDFUtils addSignature:signimg to:self.signatureData];
  114. //[self addSignature:signimg];
  115. };
  116. // orderinfoVC.url_type = URL_REMOTE;
  117. // orderinfoVC.request_url=URL_CARTDELIVERY;
  118. //
  119. // orderinfoVC.params = params;
  120. //
  121. // orderinfoVC.delegate=self;
  122. //
  123. // if(checked.count==count)
  124. // {
  125. // orderinfoVC.have_tail = true
  126. // }
  127. [self.navigationController pushViewController:vc animated:true];
  128. };
  129. signatureVC.blk_Fill =^(NSString* file)
  130. {
  131. // NSString* file_name=[file lastPathComponent];
  132. //
  133. // NSData* img_data=[ImageUtils load_img:file];
  134. //
  135. // if(img_data!=nil)
  136. // {
  137. //
  138. // UIImage* image=[UIImage imageWithData:img_data];
  139. // touchImageView.image = image;
  140. //
  141. // long index = touchImageView.tag - CONTROL_BASE;
  142. // self.controlTemplate [[NSString stringWithFormat:@"control_%ld",index] ][@"value"] = file;
  143. //
  144. // }
  145. };
  146. UINavigationController * navi = [[UINavigationController alloc] initWithRootViewController:signatureVC];
  147. navi.modalPresentationStyle=UIModalPresentationPopover;
  148. UIPopoverPresentationController* popPc = navi.popoverPresentationController;
  149. popPc.permittedArrowDirections = UIPopoverArrowDirectionUp;
  150. popPc.barButtonItem = sender;
  151. popPc.delegate = nil;
  152. [self presentViewController:navi animated:true completion:nil];
  153. //// menu.selector = self.selector;
  154. ////
  155. //// menu.selectordelegate = self;
  156. //
  157. // // 1.创建一个UIPopover
  158. // UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:[[UINavigationController alloc] initWithRootViewController:menu]];
  159. //
  160. //
  161. //
  162. // UIPopoverPresentationController
  163. // // 2.设置尺寸
  164. // // popover.popoverContentSize = CGSizeMake(320, 44 * 5);
  165. //
  166. // // 3.从哪里显示出来 --> 指向item
  167. //
  168. // [popover presentPopoverFromRect:self.view.frame inView:self.view permittedArrowDirections:0 animated:YES];
  169. // // [popover presentPopoverFromBarButtonItem:item permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  170. //
  171. //// self.popover = popover;
  172. // popover pop
  173. //
  174. // [self performSegueWithIdentifier:@"selector_popover" sender:self];
  175. }
  176. - (IBAction)onSaveClicked:(id)sender {
  177. if([self verifyData]==false)
  178. return;
  179. __weak typeof(self) weakSelf = self;
  180. NSString* pdffile = [PDFUtils SavePDF:self.controlTemplate source:self.document window_rect:CGRectMake(0, 0, 768, 960)];
  181. NSString* zipfile=[ACNetwork prepareUploadFile:pdffile json:self.controlTemplate];
  182. // return;
  183. {
  184. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Saving"];
  185. // self.uploadProgress.progress = 0;
  186. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  187. {
  188. // UIAlertView * waitalert = [[UIAlertView alloc] initWithTitle:@"wait" message:@"uploading" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
  189. // [waitalert show];
  190. // NSData *imageData = UIImagePNGRepresentation(self.imgview.image);
  191. AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  192. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  193. // if(appDelegate.user!=nil)
  194. // [params setValue:appDelegate.user forKey:@"user"];
  195. // // if(appDelegate.contact_id!=nil)
  196. // // [params setValue:appDelegate.contact_id forKey:@"contactId"];
  197. // if(appDelegate.password!=nil)
  198. // [params setValue:appDelegate.password forKey:@"password"];
  199. [params setValue:[AESCrypt AES128Encrypt:appDelegate.user key:@"usai2010"] forKey:@"user"];
  200. [params setValue:[AESCrypt AES128Encrypt:appDelegate.password key:@"usai2010"] forKey:@"password"];
  201. [params setValue:@"deviceMain" forKey:@"_operate"];
  202. UIDevice * dev = [UIDevice currentDevice];
  203. NSUUID* uuid =dev.identifierForVendor;
  204. #if TARGET_IPHONE_SIMULATOR//模拟器
  205. [params setValue:@"simulator_uuid" forKey:@"deviceid"];
  206. #elif TARGET_OS_IPHONE//真机
  207. [params setValue:uuid.UUIDString forKey:@"deviceid"];
  208. #endif
  209. NSMutableString *body=[[NSMutableString alloc]init];
  210. //参数的集合的所有key的集合
  211. NSArray *keys= [params allKeys];
  212. DebugLog(@"================parms==================");
  213. //遍历keys
  214. for(int i=0;i<[keys count];i++)
  215. {
  216. //得到当前key
  217. NSString *key=[keys objectAtIndex:i];
  218. //如果key不是pic,说明value是字符类型,比如name:Boris
  219. if(![key isEqualToString:@"pic"])
  220. {
  221. // //添加分界线,换行
  222. // [body appendFormat:@"%@\r\n",MPboundary];
  223. //添加字段名称,换2行
  224. [body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
  225. //添加字段的值
  226. [body appendFormat:@"%@\r\n",[params objectForKey:key]];
  227. DebugLog(@"parameter: key=%@ value=%@",key,[params objectForKey:key]);
  228. }
  229. }
  230. DebugLog(@"================parms==================");
  231. NSData* data = [NSData dataWithContentsOfFile: zipfile];
  232. NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_PDF parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  233. [formData appendPartWithFileData:data name:@"upfile" fileName:[zipfile lastPathComponent] mimeType:@"application/zip"];
  234. } error:nil];
  235. // NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:WEBSERVICE_URL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  236. // [formData appendPartWithFileData:imageData name:@"upfile" fileName:@"test" mimeType:@"image/png"];
  237. // } error:nil];
  238. //
  239. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  240. //manager.responseSerializer.acceptableContentTypes = [self.operationManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
  241. //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
  242. //
  243. NSProgress *progress = nil;
  244. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  245. // [progress removeObserver:self forKeyPath:@"fractionCompleted"];
  246. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  247. if (error) {
  248. NSString* err_msg = [error localizedDescription];
  249. DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  250. NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  251. DebugLog(@"data string: %@",str);
  252. [RAUtils message_alert:@"Can not connect to server please try again." title:@"Save document" controller:self] ;
  253. } else {
  254. DebugLog(@"response ");
  255. NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  256. // 再将NSData转为字符串
  257. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  258. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  259. DebugLog(@"data string: %@",jsonStr);
  260. NSDictionary* json = responseObject;
  261. if([[json valueForKey:@"result"] intValue]==2)
  262. {
  263. [weakSelf.navigationController popToRootViewControllerAnimated:false];
  264. //
  265. //
  266. if(weakSelf.OnSave)
  267. weakSelf.OnSave(pdffile);
  268. }
  269. else
  270. {
  271. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Save document" controller:self] ;
  272. }
  273. }
  274. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  275. }];
  276. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  277. //
  278. [uploadTask resume];
  279. }
  280. }
  281. // [PDFUtils savep
  282. }
  283. -(void) addSignature :(UIImage*) img to:(NSMutableDictionary*)signatureData
  284. {
  285. if(true)
  286. {
  287. NSString* newfile=[self saveTempSignature:UIImagePNGRepresentation(img)];
  288. int newidx = [self.signatureData[@"count"] intValue];
  289. // [@"file"]= newfile;
  290. NSMutableDictionary* item = [[NSMutableDictionary alloc] init];
  291. item[@"file"]=newfile;
  292. signatureData[[NSString stringWithFormat:@"item_%d",newidx]] = item;
  293. signatureData[@"count"]=[NSNumber numberWithInt:newidx+1];
  294. }
  295. }
  296. -(NSString *) saveTempSignature:(NSData *) image
  297. {
  298. NSString* tempDir = NSTemporaryDirectory();
  299. NSString *saveFileName=[NSString stringWithFormat:@"%@.png",[[NSUUID UUID ] UUIDString] ] ;
  300. NSString *newFilePath=[tempDir stringByAppendingPathComponent:saveFileName];
  301. bool bsuccess=[image writeToFile:newFilePath atomically:YES];
  302. if(bsuccess)
  303. {
  304. return newFilePath;
  305. }
  306. else
  307. {
  308. return nil;
  309. }
  310. }
  311. /*
  312. #pragma mark - Navigation
  313. // In a storyboard-based application, you will often want to do a little preparation before navigation
  314. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  315. // Get the new view controller using [segue destinationViewController].
  316. // Pass the selected object to the new view controller.
  317. }
  318. */
  319. - (PageViewController *)viewControllerAtIndex:(NSUInteger)index
  320. {
  321. //Return the PDFViewController for the given index.
  322. if ((self.total_page == 0 )|| (index > self.total_page) ) {
  323. return nil;
  324. }
  325. //Create a new view controller and pass suitable data.
  326. PageViewController *PageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PageViewController"];
  327. PageViewController.pageRef = CGPDFDocumentGetPage (self.document , index+1);
  328. PageViewController.pageIndex = index;
  329. ;
  330. PageViewController.controlTemplate = self.controlTemplate[[NSString stringWithFormat:@"page_%lu",(unsigned long)index]];
  331. PageViewController.signatureData = self.signatureData;
  332. //PageViewController.pdfview = [self.pagePDF objectAtIndex:index];
  333. /*
  334. PageViewController.pdfview = [[PDFView alloc]initWithFrame:self.view.frame atPage:index];
  335. [PageViewController.view addSubview:PageViewController.pdfview];
  336. NSLog(@"index = %d",index);
  337. */
  338. return PageViewController;
  339. }
  340. - (NSUInteger) indexOfViewController:(PageViewController *)viewController
  341. {
  342. return viewController.pageIndex;//[self.pagePDF indexOfObject:viewController.pdfview];
  343. }
  344. #pragma mark verify data
  345. - (UIView *)createDemoView:(NSString*) msg
  346. {
  347. UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 200)];
  348. // UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 270, 180)];
  349. // [imageView setImage:[UIImage imageNamed:@"demo"]];
  350. UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 270, 10)];
  351. titleLabel.font = [UIFont boldSystemFontOfSize:20];
  352. titleLabel.text=@"Please complete missing fields:";
  353. titleLabel.numberOfLines = 0;
  354. titleLabel.lineBreakMode =NSLineBreakByWordWrapping;
  355. // titleLabel.textAlignment = NSTextAlignmentRight;
  356. titleLabel.textAlignment = NSTextAlignmentCenter;
  357. CGSize constraintkey = CGSizeMake(270, 20000.0f);//key label width is 40% cell width;
  358. CGSize sizetitle = [@"Please complete missing fields:" sizeWithFont:[UIFont systemFontOfSize:20.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  359. //[titleLabel sizeToFit];
  360. // titleLabel.center.x = demoView.center.x;
  361. titleLabel.frame = CGRectMake(10, 10, 270, sizetitle.height);
  362. [demoView addSubview:titleLabel];
  363. UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 260, 10)];
  364. lineLabel.font = [UIFont boldSystemFontOfSize:17];
  365. lineLabel.text=msg;
  366. lineLabel.numberOfLines = 0;
  367. lineLabel.lineBreakMode =NSLineBreakByWordWrapping;
  368. CGSize sizemsg = [msg sizeWithFont:[UIFont boldSystemFontOfSize:17.0] constrainedToSize:constraintkey lineBreakMode:NSLineBreakByWordWrapping];
  369. lineLabel.frame = CGRectMake(15, titleLabel.frame.origin.y+ titleLabel.frame.size.height+20, 260, sizemsg.height);
  370. //[lineLabel sizeToFit];
  371. [demoView addSubview:lineLabel];
  372. demoView.frame = CGRectMake(0, 0, 290, lineLabel.frame.origin.y+lineLabel.frame.size.height+25);
  373. return demoView;
  374. }
  375. -(bool) verifyData
  376. {
  377. return true;
  378. bool ret=true;
  379. int more=0;
  380. NSMutableArray* fields=[[NSMutableArray alloc] init];
  381. for(int i=0;i<[self.controlTemplate[@"count"] intValue];i++)
  382. {
  383. NSMutableDictionary* page =self.controlTemplate[[NSString stringWithFormat:@"page_%d",i]];
  384. for(int j=0;j<[page[@"count"] intValue];j++)
  385. {
  386. NSMutableDictionary* control =page[[NSString stringWithFormat:@"control_%d",j]];
  387. NSString* type = control[@"type"];
  388. NSString* name = control[@"aname"];
  389. bool required = [control[@"required"] boolValue];
  390. if(required==false)
  391. continue;
  392. if([type isEqualToString:@"Check"])
  393. {
  394. NSArray* arr = control[@"value"];
  395. if(arr.count==0)
  396. {
  397. ret=false;
  398. NSString* msg=[NSString stringWithFormat:@"%u Page %d %@ is missing.",fields.count+1,i+1,name];
  399. if(fields.count<15)
  400. [fields addObject:msg];
  401. else
  402. more++;
  403. }
  404. }
  405. else if(/*[type isEqualToString:@"Signature"]||*/[type isEqualToString:@"Image"]||[type isEqualToString:@"Label"])
  406. {
  407. continue;
  408. }
  409. else
  410. {
  411. NSString* str = control[@"value"];
  412. if(str.length==0)
  413. {
  414. ret=false;
  415. NSString* msg=[NSString stringWithFormat:@"%u Page %d %@ is missing.",fields.count+1,i+1,name];
  416. if(fields.count<15)
  417. [fields addObject:msg];
  418. else
  419. more++;
  420. }
  421. }
  422. }
  423. }
  424. if(ret==false)
  425. {
  426. if(more>0)
  427. {
  428. NSString* msg=[NSString stringWithFormat:@"And %d more missing field.",more];
  429. [fields addObject:@"......"];
  430. [fields addObject:msg];
  431. }
  432. NSString* missfields=[fields componentsJoinedByString:@"\n"];
  433. NSString* msg = [NSString stringWithFormat:@"%@",missfields];
  434. CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
  435. // Add some custom content to the alert view
  436. [alertView setContainerView:[self createDemoView:msg]];
  437. // Modify the parameters
  438. [alertView setButtonTitles:[NSMutableArray arrayWithObjects:@"OK", nil]];
  439. //[alertView setDelegate:self];
  440. // You may use a Block, rather than a delegate.
  441. [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
  442. // DebugLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, (int)[alertView tag]);
  443. [alertView close];
  444. }];
  445. // [alertView setUseMotionEffects:true];
  446. // And launch the dialog
  447. [alertView sizeToFit];
  448. [alertView show];
  449. }
  450. return ret;
  451. }
  452. #pragma mark - Page View Controller Data Source
  453. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
  454. {
  455. NSUInteger index = [self indexOfViewController:(PageViewController *)viewController];
  456. if ((index == 0 ) || (index == NSNotFound)){
  457. return nil;
  458. }
  459. index--;
  460. return [self viewControllerAtIndex:index];
  461. }
  462. - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
  463. {
  464. NSUInteger index = [self indexOfViewController:(PageViewController *)viewController];
  465. if (index == NSNotFound)
  466. {
  467. return nil;
  468. }
  469. index++;
  470. if (index == self.total_page){
  471. return nil;
  472. }
  473. return [self viewControllerAtIndex:index];
  474. }
  475. #pragma mark - UIPageViewController delegate methods
  476. - (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation {
  477. if (true||UIInterfaceOrientationIsPortrait(orientation) || ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)) {
  478. // In portrait orientation or on iPhone: Set the spine position to "min" and the page view controller's view controllers array to contain just one view controller. Setting the spine position to 'UIPageViewControllerSpineLocationMid' in landscape orientation sets the doubleSided property to YES, so set it to NO here.
  479. UIViewController *currentViewController = self.pageViewController.viewControllers[0];
  480. NSArray *viewControllers = @[currentViewController];
  481. [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
  482. self.pageViewController.doubleSided = NO;
  483. return UIPageViewControllerSpineLocationMin;
  484. }
  485. // // In landscape orientation: Set set the spine location to "mid" and the page view controller's view controllers array to contain two view controllers. If the current page is even, set it to contain the current and next view controllers; if it is odd, set the array to contain the previous and current view controllers.
  486. // PageViewController *currentViewController = self.pageViewController.viewControllers[0];
  487. // NSArray *viewControllers = nil;
  488. //
  489. // NSUInteger indexOfCurrentViewController = [self indexOfViewController:currentViewController];
  490. // if (indexOfCurrentViewController == 0 || indexOfCurrentViewController % 2 == 0) {
  491. // UIViewController *nextViewController = [self pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController];
  492. // viewControllers = @[currentViewController, nextViewController];
  493. // } else {
  494. // UIViewController *previousViewController = [self pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController];
  495. // viewControllers = @[previousViewController, currentViewController];
  496. // }
  497. // [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
  498. //
  499. //
  500. // return UIPageViewControllerSpineLocationMid;
  501. }
  502. @end