|
//
// TPImageScannerController.m
// ThePaperHD
//
// Created by scar1900 on 15/3/5.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "TPImageScannerController.h"
#import "ATPagingView.h"
#import "AsyncImageScrollView.h"
#import "hotImageListController.h"
#import "MLNavigationController.h"
@interface TPImageScannerController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate,AsyImageScrollDelegate> {
NSInteger lastPageIndex;
BOOL disableUserInterface;
UISwipeGestureRecognizer *leftSwipe;
}
@property(nonatomic, strong)UIPageViewController *pageViewController;
@end
@implementation TPImageScannerController
@synthesize imageBOList = _imageBOList;
@synthesize hotList = _hotList;
@synthesize type;
@synthesize delegate;
@synthesize currentIndex = _currentIndex;
@synthesize gradientLayer;
@synthesize rightGesture;
@synthesize rightSwipe;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blackColor];
if (isIOS7) {
self.automaticallyAdjustsScrollViewInsets = NO;
[self setNeedsStatusBarAppearanceUpdate];
}
[self disablePopBackGesture];
[self.view addSubview:self.toolBarView];
[self.view addSubview:self.backBtn];
[self.toolBarView addSubview:self.numLabel];
[self.toolBarView addSubview:self.saveButton];
disableUserInterface = NO;
// self.rightGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(rightPopSwipeGestureHandler:)];
// [self.rightGesture delaysTouchesBegan];
// self.rightGesture .numberOfTouchesRequired = 1;
// self.rightGestuhotListre.delegate = self;
// self.rightGesture .direction = UISwipeGestureRecognizerDirectionRight;
// // self.rightGesture.enabled = NO;
// [self.view addGestureRecognizer:self.rightGesture ];
if (type == contentImageScannerType) {//图集模式
[MobClick event:@"5"];
}else {//图集新闻浏览
[MobClick event:@"3"];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES; // Recognizers of this class are the first priority
}
- (void)rightPopSwipeGestureHandler:(id)sender {
if (self.currentIndex != 0) {
return;
}
AsyncImageScrollView *imageView = self.pagesList[0];
if (imageView.scrollView.dragging || imageView.scrollView.decelerating || imageView.scrollView.zooming) {
return;
}
if (!self.isPresent) {
[self.navigationController popViewControllerAnimated:YES];
}else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)setCurrentIndex:(NSInteger)index {
_currentIndex = index;
dispatch_async(dispatch_get_main_queue(), ^{
self.numLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)index+1,(long)self.imageBOList.count];
});
self.toolBarView.frame = CGRectMake(0,
CGRectGetHeight(self.view.bounds)-100,
CGRectGetWidth(self.view.bounds),
100);
self.numLabel.frame = CGRectMake(CGRectGetWidth(self.toolBarView.bounds)/2-190/4,
CGRectGetHeight(self.toolBarView.bounds)-136/2,
190/2,
40);
self.saveButton.frame = CGRectMake(CGRectGetWidth(self.toolBarView.frame)-15-55/2,
CGRectGetHeight(self.toolBarView.frame)-126/2,
55/2,
59/2);
self.gradientLayer.frame = self.toolBarView.bounds;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (UIView*)toolBarView {
if (!_toolBarView) {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat height = 0;
CGFloat width = 0;
if (orientation == UIInterfaceOrientationLandscapeRight
|| orientation ==UIInterfaceOrientationLandscapeLeft) {
width = 1024;
height = 768;
}else {
height = 1024;
width = 768;
}
_toolBarView = [[UIView alloc]initWithFrame:CGRectMake(0,
height-100,
width,
100)];
_toolBarView.backgroundColor = [UIColor clearColor];
gradientLayer = [CAGradientLayer layer];
gradientLayer.borderWidth = 0;
gradientLayer.frame = _toolBarView.bounds;
gradientLayer.colors = @[(id)[UIColor clearColor].CGColor,(id)[UIColor blackColor].CGColor];
[_toolBarView.layer insertSublayer:gradientLayer atIndex:0];
}
return _toolBarView;
}
- (UIButton*)backBtn {
if (!_backBtn) {
_backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_backBtn.frame = CGRectMake(20,
CGRectGetMaxY(self.toolBarView.frame)-80,
50,
80);
[_backBtn setImage:Image(@"login/popUpBack.png") forState:UIControlStateNormal];
[_backBtn setImage:Image(@"login/popUpBack_h.png") forState:UIControlStateHighlighted];
[_backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 30, 0)];
[_backBtn addTarget:self action:@selector(backHanderEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _backBtn;
}
- (UIButton*)saveButton {
if (!_saveButton) {
_saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
_saveButton.frame = CGRectMake(CGRectGetWidth(self.toolBarView.frame)-15-55/2,
CGRectGetHeight(self.toolBarView.frame)-142/2,
55/2,
59/2);
[_saveButton setImage:Image(@"Button/saveButton.png") forState:UIControlStateNormal];
[_saveButton addTarget:self action:@selector(saveHanderEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _saveButton;
}
- (UILabel*)numLabel {
if (!_numLabel) {
_numLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.toolBarView.bounds)/2-190/4,
CGRectGetHeight(self.toolBarView.bounds)-80,
190/2,
50)];
_numLabel.textColor = [UIColor whiteColor];
_numLabel.font = appFont(15, NO);
_numLabel.backgroundColor = [UIColor clearColor];
_numLabel.textAlignment = NSTextAlignmentCenter;
}
return _numLabel;
}
- (void)setImageBOList:(NSMutableArray *)list {
__block NSInteger invalidIndex = -1;
[list enumerateObjectsUsingBlock:^(imageObjectBO* obj, NSUInteger idx, BOOL *stop) {
if (isBlankString(obj.tags)) {
invalidIndex = idx;
}
}];
if (invalidIndex >= 0) {
[list removeObjectAtIndex:invalidIndex];
}
_imageBOList = list;
self.pagesList = [NSMutableArray array];
[self setPageViews];
}
-(void)setHotList:(NSMutableArray *)list{
_hotList = list;
}
- (void)setPageViews {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight
|| orientation ==UIInterfaceOrientationLandscapeLeft) {
self.view.frame = CGRectMake(0, 0, 1024, 768);
}else {
self.view.frame = CGRectMake(0, 0, 768, 1024);
}
self.pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;
self.pageViewController.view.frame = self.view.bounds;
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.view insertSubview:self.pageViewController.view belowSubview:self.toolBarView];
[self setupImageInPage];
__weak typeof(self) Self = self;
[self.pageViewController setViewControllers:@[self.pagesList[self.currentIndex]] direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:^(BOOL finished) {
if (type == contentImageScannerType) {
AsyncImageScrollView *imageScrollView = Self.pagesList[Self.currentIndex];
imageObjectBO *imageBO = Self.imageBOList[Self.currentIndex];
NSString *url = imageBO.url;
imageScrollView.imageUrl = url;
imageScrollView.imageId = getImageNameFromURL(url);
}else{//【需求】图集,热门图集未实现(bug:3443)
if ([Self.pagesList[Self.currentIndex] isKindOfClass:[hotImageListController class]]) {
}else{
AsyncImageScrollView *imageScrollView = Self.pagesList[Self.currentIndex];
imageObjectBO *imageBO = Self.imageBOList[Self.currentIndex];
NSString *url = imageBO.url;
imageScrollView.imageUrl = url;
imageScrollView.imageId = getImageNameFromURL(url);
}
}
}];
// if (self.currentIndex == 0) {
// self.rightGesture.enabled = YES;
// }else self.rightGesture.enabled = NO;
}
- (void)setupImageInPage {
self.pagesList = [NSMutableArray array];
[self.imageBOList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
AsyncImageScrollView *imageScrollView = [[AsyncImageScrollView alloc]init];
if (self.imageBOList.count > 1) {
imageScrollView.isListMore = YES;
}else imageScrollView.isListMore = NO;
imageScrollView.imageDelegate = self;
// imageScrollView.Frame = self.pageViewController.view.bounds;
imageScrollView.index = idx;
imageObjectBO *imageBO = self.imageBOList[self.currentIndex];
CGSize imageSize = CGSizeMake([imageBO.width floatValue], [imageBO.height floatValue]);
imageScrollView.self.imageSize = imageSize;
[self addChildViewController:imageScrollView];
[self.pagesList addObject:imageScrollView];
}];
if (type == imageScanType) {//【需求】图集,热门图集未实现(bug:3443)
rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeHandler:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
rightSwipe.delegate = self;
rightSwipe.enabled = NO;
[self.view addGestureRecognizer:rightSwipe];
leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandler:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionRight;
leftSwipe.enabled = YES;
leftSwipe.delegate = self;
[self.view addGestureRecognizer:leftSwipe];
}
}
- (void)swipeHandler:(id)sender {//左滑
((MLNavigationController *)self.navigationController).canDragBack = YES;
hotImageListController *hotListVC = [[hotImageListController alloc] init];
hotListVC.hotList = self.hotList;
[self.navigationController pushViewController:hotListVC animated:YES];
}
- (void)leftSwipeHandler:(id)sender {//右滑
[self backHanderEvent:self.backBtn];
}
- (void)statusBarOrientationChange:(NSNotification *)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight
|| orientation ==UIInterfaceOrientationLandscapeLeft) {
self.view.frame = CGRectMake(0, 0, 1024, 768);
}else {
self.view.frame = CGRectMake(0, 0, 768, 1024);
}
self.pageViewController.view.bounds = self.view.bounds;
self.currentIndex = self.currentIndex;
}
#pragma mark - page controller delegate and datasource
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController
viewControllerAfterViewController:(UIViewController *)viewController {
if (type == contentImageScannerType) {
NSInteger index = ((AsyncImageScrollView*)viewController).index;
if (index == self.pagesList.count-1) {
return nil;
}
index = ((AsyncImageScrollView*)viewController).index+1;
AsyncImageScrollView *vc = self.pagesList[index];
return vc;
}else{//【需求】图集,热门图集未实现(bug:3443)
NSInteger index = -1;
if ([viewController isKindOfClass:[hotImageListController class]]) {
}else{
index = ((AsyncImageScrollView*)viewController).index;
}
if (index == -1) {
return nil;
}else{
if ([viewController isKindOfClass:[hotImageListController class]]) {
self.toolBarView.hidden = YES;
return viewController;
}else{
if(index == self.pagesList.count - 1){
return nil;
}else{
index = ((AsyncImageScrollView*)viewController).index+1;
AsyncImageScrollView *vc = self.pagesList[index];
// self.toolBarView.hidden = NO;// 图集:图说屏蔽后,上滑或者下滑浏览其他的图片时,图说依然会被自动打开(bug:4466)
return vc;
}
}
}
}
}
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController
viewControllerBeforeViewController:(UIViewController *)viewController {
if (type == contentImageScannerType) {
NSInteger index = ((AsyncImageScrollView*)viewController).index;
if (index ==0) {
return nil;
}
index = ((AsyncImageScrollView*)viewController).index-1;
AsyncImageScrollView *vc = self.pagesList[index];
return vc;
}else{//【需求】图集,热门图集未实现(bug:3443)
NSInteger index;
if ([viewController isKindOfClass:[hotImageListController class]]) {
index = self.pagesList.count -1 ;
}else{
index = ((AsyncImageScrollView*)viewController).index;
}
if (index == 0) {
return nil;
}else{
if ([viewController isKindOfClass:[hotImageListController class]]) {
index = self.pagesList.count -2;
self.toolBarView.hidden = YES;
return viewController;
}else{
// self.toolBarView.hidden = NO;// 图集:图说屏蔽后,上滑或者下滑浏览其他的图片时,图说依然会被自动打开(bug:4466)
index = ((AsyncImageScrollView*)viewController).index-1;
AsyncImageScrollView *vc = self.pagesList[index];
return vc;
}
}
}
}
- (void)pageViewController:(UIPageViewController *)pageViewController
didFinishAnimating:(BOOL)finished
previousViewControllers:(NSArray *)previousViewControllers
transitionCompleted:(BOOL)completed {
if (completed) {
//[self.pageViewController.viewControllers[0] isKindOfClass:[hotImageListController new]]
if ([self.pageViewController.viewControllers[0] isKindOfClass:[hotImageListController class]]) {//【需求】图集,热门图集未实现(bug:3443)
self.toolBarView.hidden = YES;
}else{
// self.toolBarView.hidden = NO;// 图集:图说屏蔽后,上滑或者下滑浏览其他的图片时,图说依然会被自动打开(bug:4466)
AsyncImageScrollView *vc = self.pageViewController.viewControllers[0];
if (isBlankString(vc.imageId)) {
imageObjectBO *imageBO = self.imageBOList[vc.index];
NSString *url = imageBO.url;
vc.imageUrl = url;
vc.imageId = getImageNameFromURL(url);
CGSize gifSize = CGSizeMake([imageBO.width floatValue], [imageBO.height floatValue]);
vc.self.imageSize = gifSize;
}
self.currentIndex = vc.index;
if (self.currentIndex != lastPageIndex) {
AsyncImageScrollView *imageView = self.pagesList[lastPageIndex];
[imageView rechangeInitRdct];
disableUserInterface = NO;
if ([[UIApplication sharedApplication]isIgnoringInteractionEvents]) {
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
lastPageIndex = self.currentIndex;
}
}
if (self.currentIndex == 0) {
leftSwipe.enabled = YES;
}else {
leftSwipe.enabled = NO;
}
if (type == imageScanType && self.currentIndex == self.pagesList.count-1) {
rightSwipe.enabled = YES;
for(UIView* view in self.pageViewController.view.subviews){
if([view isKindOfClass:[UIScrollView class]]){
UIScrollView* scrollView=(UIScrollView*)view;
scrollView.bounces = NO;
}
}
}else {
rightSwipe.enabled = NO;
for(UIView* view in self.pageViewController.view.subviews){
if([view isKindOfClass:[UIScrollView class]]){
UIScrollView* scrollView=(UIScrollView*)view;
scrollView.bounces = YES;
}
}
}
}
}
#pragma mark - button event
- (void)backHanderEvent:(UIButton*)btn {
if (type == contentImageScannerType) {//图集模式
imageObjectBO *imageBO = self.imageBOList[self.currentIndex];
if ([self.delegate respondsToSelector:@selector(imageContentClick:)]) {
[self.delegate imageContentClick:imageBO];
}
}else {//图集新闻浏览
if (!self.isPresent) {
[self.navigationController popViewControllerAnimated:YES];
}else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
}
- (void)saveHanderEvent:(UIButton*)btn {
if (!self.imageBOList || self.imageBOList.count == 0) {
return;
}
AsyncImageScrollView *asyImageView = self.pagesList[self.currentIndex];
if (!asyImageView.gifData) {
if (!asyImageView.imageView.image) {
ShowTextMessage(@"图片尚未下载完成");
return;
}
UIImageWriteToSavedPhotosAlbum(asyImageView.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}else {
UIImage *gifImage = [UIImage imageWithData:asyImageView.gifData];
if (!gifImage) {
ShowTextMessage(@"图片尚未下载完成");
return;
}
UIImageWriteToSavedPhotosAlbum(gifImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
}
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
NSString *msg = nil ;
if(error != NULL){
msg = @"保存图片失败" ;
}else{
msg = @"已保存至相册" ;
}
ShowTextMessage(msg);
}
#pragma mark - imageView delegate
- (void)singleTapGestureHandler {
if (!self.imageBOList || self.imageBOList.count == 0) {
return;
}
imageObjectBO *imageBO = self.imageBOList[self.currentIndex];
if (type == contentImageScannerType) {//图集模式
if ([self.delegate respondsToSelector:@selector(imageContentClick:)]) {
[self.delegate imageContentClick:imageBO];
}
}else {//图集新闻浏览
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
|