1.h 文件
#import <UIKit/UIKit.h>
#import "BaseViewController.h"
/**
* 图片查看视图
**/
typedef void (^EditPhotosCompleteBlock)(NSInteger index);
@interface EditPhotosController : BaseViewController
@property (retain, nonatomic) NSMutableArray *imageArray; //展示图片数组
@property (assign, nonatomic) NSInteger selectIndex; //选中的下标
@property (copy, nonatomic) EditPhotosCompleteBlock editPhotosComplete;
@end
2.m文件
#import "EditPhotosController.h"
#import "ShowJoyAlterView.h"
@interface EditPhotosController ()<UIScrollViewDelegate>
@property (strong ,nonatomic) UIScrollView *imageScrollView;
@property (strong ,nonatomic) UILabel *mLabTitle;
@end
@implementation EditPhotosController
@synthesize imageArray;
@synthesize selectIndex;
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = UIColorFromRGB(0X1B1A1F);
[self addviewNavagationBar];
}
- (void)viewDidAppear:(BOOL)animated
{
[self initScrollView];
[self updateDatas:imageArray];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Init
// 初始化navagationBar
-(void)addviewNavagationBar{
UIImageView * mviewNavigationBar = [self addNavagationBar];
[self.view addSubview:mviewNavigationBar];
// 删除
UIImageView *mImgView = [self addImageView:CGRectMake(Screen_width - 35 , IOS7_OR_LATER?25:(44-21)/2, 20 , 21) withImageStr:@"NavigationBarDelete"];
mImgView.userInteractionEnabled = YES;
[mviewNavigationBar addSubview:mImgView];
UITapGestureRecognizer *mImageViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(mImageViewBroswerDelete)];
[mImgView addGestureRecognizer:mImageViewTap];
// title
self.mLabTitle = [self addLableText:CGRectMake(50, IOS7_OR_LATER?29:(44-21)/2, Screen_width-100, 18 * BEI6) withLabText:@"" withLabFont:[UIFont fontWithName:HeitiSCLight size:17.5 * BEI6] withTextColor:[UIColor whiteColor]];
self.mLabTitle.textAlignment=NSTextAlignmentCenter;
[mviewNavigationBar addSubview:self.mLabTitle];
// 返回
UIButton* mBtnLeftItem = [self.view addButtton:CGRectMake(0, IOS7_OR_LATER?29:(44-21)/2, 46, 20) title:nil target:self action:@selector(backClick) tag:001 withFont:nil];
[mBtnLeftItem setImage:[UIImage imageNamed:@"DRPersonNext"] forState:UIControlStateNormal];
mBtnLeftItem.imageEdgeInsets = UIEdgeInsetsMake(0, 11, 0, 21);
[mviewNavigationBar addSubview:mBtnLeftItem];
}
-(UIImageView *)addNavagationBar{
UIImageView * mImageviewNavigationBar = [[UIImageView alloc] init];
mImageviewNavigationBar.frame=CGRectMake(0, 0, Screen_width, IOS7_OR_LATER?64:44);
mImageviewNavigationBar.userInteractionEnabled=YES;
mImageviewNavigationBar.backgroundColor = Distribute1;
return mImageviewNavigationBar;
}
- (void)backClick
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)initScrollView
{
self.imageScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0 , 64, Screen_width, Screen_height)];
self.imageScrollView.pagingEnabled = YES;
self.imageScrollView.delegate = self;
[self.view addSubview:self.imageScrollView];
self.imageScrollView.contentOffset = CGPointMake(self.view.frame.size.width * self.selectIndex, 0);
}
#pragma mark - EditPhotos
- (void)updateDatas:(NSMutableArray *)tempImageArray
{
self.mLabTitle.text = [NSString stringWithFormat:@"%ld/%lu",self.selectIndex+1,(unsigned long)[self.imageArray count]];
[self removeAllSubViewsOfView:self.imageScrollView];
int height = 480;
if (self.view.frame.size.height == 568) {
height = 568;
}
self.imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width*[tempImageArray count],height-64);
UIImageView *photoView;
for (int i = 0; i < [tempImageArray count] ; i++ ) {
photoView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width * i, 42 * BEI6, self.view.frame.size.width, height)];
[photoView setImage:[tempImageArray objectAtIndex:i]];
photoView.contentMode = UIViewContentModeScaleAspectFit;
[self.imageScrollView addSubview:photoView];
}
}
#pragma mark - NavigationBarButtonAction
//删除
- (void)mImageViewBroswerDelete
{
if (self.imageArray.count == 1) {
[self.view showSGInfoAlert:@"不能再删除了哦~"];
}else{
ShowJoyAlterView * alterView = [[ShowJoyAlterView alloc] initWithTitle:@"提示" withDetailFirst:@"是否删除该照片?" withDetailSecond:@"" withLeftButtonTitle:@"取消" withRightButtonTitle:@"确定"];
alterView.rightBlock = ^(){
[self.imageArray removeObjectAtIndex:self.selectIndex];
self.editPhotosComplete(self.selectIndex);
if (self.selectIndex != 0) {
self.selectIndex --;
}
[self updateDatas:self.imageArray];
};
[alterView show];
}
}
#pragma makr - ScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
selectIndex = self.imageScrollView.contentOffset.x/self.view.frame.size.width;
self.mLabTitle.text = [NSString stringWithFormat:@"%ld/%lu",self.selectIndex+1,(unsigned long)[self.imageArray count]];
}
#pragma mark - Util
- (void)removeAllSubViewsOfView:(UIView *)superView
{
for (UIView *tempView in [superView subviews]) {
[tempView removeFromSuperview];
}
}
@end