|
//
// 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"
@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 enablePan;
@synthesize startTouch;
@synthesize firstPan;
@synthesize rightSwipe;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor blackColor];
enablePan = NO;
if (isIOS7) {
self.automaticallyAdjustsScrollViewInsets = NO;
[self setNeedsStatusBarAppearanceUpdate];
}
[self.view addSubview:self.toolBarView];
[self.toolBarView addSubview:self.backMaskImageView];
[self.backMaskImageView makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.toolBarView.left);
make.right.equalTo(self.toolBarView.right);
make.top.equalTo(self.toolBarView.top);
make.height.mas_equalTo(133.5);
}];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
[self.toolBarView addSubview:self.backBtn];
[self.toolBarView addSubview:self.numLabel];
[self.toolBarView addSubview:self.saveButton];
[self layoutSubViewsForButton];
disableUserInterface = NO;
// UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self
// action:@selector(paningGestureReceive:)];
// recognizer.delegate = self;
// recognizer.direction = UISwipeGestureRecognizerDirectionRight;
// [recognizer delaysTouchesBegan];
// [self.view addGestureRecognizer:recognizer];
if (type == contentImageScannerType) {//图集模式
}else {//图集新闻浏览
}
}
- (void)paningGestureReceive:(UIGestureRecognizer *)pan {
if (self.currentIndex != 0) {
return;
}
if (!enablePan) {
return;
}
AsyncImageScrollView *imageView = self.pagesList[0];
if (imageView.scrollView.isDragging || imageView.scrollView.isDecelerating || imageView.scrollView.isZooming) {
return;
}
if (imageView.scrollView.zoomScale != 1) {
return;
}
CGPoint touchPoint = [pan locationInView:KEY_WINDOW];
// begin paning, show the backgroundView(last screenshot),if not exist, create it.
if (pan.state == UIGestureRecognizerStateBegan) {
startTouch = touchPoint;
firstPan = YES;
}else if (pan.state == UIGestureRecognizerStateChanged) {
if (startTouch.x < touchPoint.x) {
if ([self.delegate respondsToSelector:@selector(imageContentClick:)] && firstPan) {
[UIView animateWithDuration:0.25 animations:^{
self.shouldV = YES;
[[UIApplication sharedApplication]
setStatusBarOrientation:UIInterfaceOrientationPortrait
animated:NO];
[[UIDevice currentDevice]
setValue:[NSNumber
numberWithInteger:UIInterfaceOrientationPortrait]
forKey:@"orientation"];
} completion:^(BOOL finished) {
[self.delegate imageContentClick:nil];
}];
firstPan = NO;
}
return;
}else {
return;
}
}
// [(MLNavigationController*)self.navigationController paningGestureReceive:pan];
}
- (void)layoutSubViewsForButton
{
[self.toolBarView makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view.left);
make.right.mas_equalTo(self.view.right);
make.top.mas_equalTo(self.view.top);
make.height.mas_equalTo(65);
}];
[self.numLabel makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.toolBarView.right).offset(-10);
make.bottom.mas_equalTo(self.toolBarView.bottom);
make.height.mas_equalTo(30);
make.width.mas_equalTo(50);
}];
[self.saveButton makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self.numLabel.left).offset(-10);
make.bottom.mas_equalTo(self.toolBarView.bottom);
make.height.mas_equalTo(30);
make.width.mas_equalTo(30);
}];
[self.backBtn makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.toolBarView.left).offset(10);
make.bottom.mas_equalTo(self.toolBarView.bottom);
make.height.mas_equalTo(30);
make.width.mas_equalTo(30);
}];
}
- (BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return self.shouldV ? UIInterfaceOrientationMaskPortrait: UIInterfaceOrientationMaskAllButUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
if ([otherGestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
return NO;
}else return YES;
}
- (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;
}
[self.navigationController popViewControllerAnimated:YES];
}
- (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,
// 20,
// CGRectGetWidth(self.view.bounds),
// 45);
//
// self.numLabel.frame = CGRectMake(CGRectGetWidth(self.toolBarView.bounds)-70,
// CGRectGetHeight(self.toolBarView.bounds)-136/2,
// 190/2,
// 40);
//
// self.saveButton.frame = CGRectMake(CGRectGetWidth(self.toolBarView.frame)-15-55/2 - 35,
// CGRectGetHeight(self.toolBarView.frame)-126/2,
// 55/2,
// 59/2);
self.gradientLayer.frame = self.toolBarView.bounds;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
((MLNavigationController *)self.navigationController).canDragBack = NO;
self.navigationController.navigationBarHidden = YES;
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (UIImageView *)backMaskImageView {
if (!_backMaskImageView) {
_backMaskImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
_backMaskImageView.image = Image(@"imageList/BackForScan.png");
_backMaskImageView.userInteractionEnabled = NO;
}
return _backMaskImageView;
}
- (UIView*)toolBarView {
if (!_toolBarView) {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGFloat height = 0;
CGFloat width = 0;
if (orientation == UIInterfaceOrientationLandscapeRight
|| orientation ==UIInterfaceOrientationLandscapeLeft) {
width = rect_screen.size.width;
height = rect_screen.size.height;
}else {
height = rect_screen.size.height;
width = rect_screen.size.width;
}
_toolBarView = [[UIView alloc]init];
_toolBarView.backgroundColor = [UIColor clearColor];
}
return _toolBarView;
}
- (UIButton*)backBtn {
if (!_backBtn) {
_backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_backBtn.frame = CGRectMake(10,
CGRectGetHeight(self.toolBarView.frame)-65,
30,
30);
[_backBtn setImage:Image(@"imageList/backBtn.png") forState:UIControlStateNormal];
[_backBtn setImageEdgeInsets:UIEdgeInsetsMake(CGRectGetHeight(_backBtn.bounds)/2-20/2,
CGRectGetWidth(_backBtn.bounds)/2-20/2,
CGRectGetHeight(_backBtn.bounds)/2-20/2,
CGRectGetWidth(_backBtn.bounds)/2-20/2)];
[_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 - 100,
CGRectGetHeight(self.toolBarView.frame)-142/2,
55/2,
59/2);
[_saveButton setImage:Image(@"imageList/downLoadBtn.png") forState:UIControlStateNormal];
[_saveButton setImage:Image(@"imageList/downLoadBtnHighLight.png") forState:UIControlStateHighlighted];
[_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(TEXT_FOUR_LEVELSIZE, 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 {
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.gestureRecognizers = self.pageViewController.gestureRecognizers;
[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)
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) {
enablePan = YES;
}else {
enablePan = 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.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];
// [self addChildViewController:hotListVC];
// [self.pagesList addObject:hotListVC];
}
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)statusBarOrientationChange:(NSNotification *)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight
|| orientation ==UIInterfaceOrientationLandscapeLeft) {
[self.toolBarView remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view.left);
make.right.mas_equalTo(self.view.right);
make.top.mas_equalTo(self.view.top);
make.height.mas_equalTo(45);
}];
}else {
[self.toolBarView remakeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.view.left);
make.right.mas_equalTo(self.view.right);
make.top.mas_equalTo(self.view.top).offset(20);
make.height.mas_equalTo(45);
}];
}
// if (![self.pageViewController.viewControllers[0] isKindOfClass:[hotImageListController class]]) {//【需求】图集,热门图集未实现(bug:3443)
// self.pageViewController.view.bounds = self.view.bounds;
// self.descView.hidden = YES;
// self.currentIndex = self.currentIndex;
// }
// else {
// // self.descView.hidden = NO;
// }
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
- (void)leftSwipeHandler:(id)sender {
if (self.currentIndex != 0) {
return;
}
[self backHanderEvent:self.backBtn];
}
#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;
self.descView.hidden = YES;
return viewController;
}else{
if (index == self.pagesList.count-1) {
return nil;
}
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]]
// self.toolBarView.hidden = NO;// 图集:图说屏蔽后,上滑或者下滑浏览其他的图片时,图说依然会被自动打开(bug:4466)
if ([self.pageViewController.viewControllers[0] isKindOfClass:[hotImageListController class]]) {//【需求】图集,热门图集未实现(bug:3443)
self.toolBarView.hidden = YES;
self.descView.hidden = YES;
}else{
self.toolBarView.hidden = NO;
self.descView.hidden = NO;
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 {
[self singleTapGestureHandler];
}
- (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:)]) {
[UIView animateWithDuration:0.25 animations:^{
self.shouldV = YES;
[[UIApplication sharedApplication]
setStatusBarOrientation:UIInterfaceOrientationPortrait
animated:NO];
[[UIDevice currentDevice]
setValue:[NSNumber
numberWithInteger:UIInterfaceOrientationPortrait]
forKey:@"orientation"];
} completion:^(BOOL finished) {
[self.delegate imageContentClick:imageBO];
}];
}
}else {//图集新闻浏览
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
|