iOS开发总结-图片左右滑动浏览

时间:2021-01-14 04:17:05

iOS开发总结-图片左右滑动浏览

//
// PicViewController.m
// BJ
//
// Created by shirenfeng on 16/9/13.
// Copyright © 2016年 com.zybjw. All rights reserved.
// 对比图片浏览
#import "FTMacro.h"
#import "Utilities.h"
#import "PicViewController.h"
#import "DBStrore.h"
//#define kWidthOfScreen [[UIScreen mainScreen] bounds].size.width
//#define kHeightOfScreen ([[UIScreen mainScreen] bounds].size.height-50)
#define kImageViewCount 3
// scrollview Y
#define kScrVStarty 65 // shopname height
#define kShopNameLabelHeight 56
#define kScrVImgStarty 30
#define kScrVHeight (kScreenHeight-kScrVStarty)
// goodsurlbutton width
#define kGoodsUrlBtnWidth 60
// goodsurlbutton height
#define kGoodsUrlBtnHeight 35 @interface PicViewController ()
/**
* 加载图片数据
*/
- (void)loadImageData; /**
* 添加滚动视图
*/
- (void)addScrollView; /**
* 添加三个图片视图到滚动视图内
*/
- (void)addImageViewsToScrollView; /**
* 添加分页控件
*/
- (void)addPageControl; /**
* 根据当前图片索引设置信息
*
* @param currentImageIndex 当前图片索引;即中间
*/
- (void)setInfoByCurrentImageIndex:(NSUInteger)currentImageIndex; /**
* 设置默认信息
*/
- (void)setDefaultInfo; /**
* 重新加载图片
*/
- (void)reloadImage; @end @implementation PicViewController
-(void)initView
{
[self loadImageData];
if(_imageCount > ){ UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setTitle:@"删除图片" forState:UIControlStateNormal];
[rightButton setTitleColor:self.view.tintColor forState:UIControlStateNormal];
rightButton.titleLabel.font = [UIFont systemFontOfSize:];
[rightButton addTarget:self action:@selector(deletePic) forControlEvents:UIControlEventTouchUpInside];
[rightButton setFrame:CGRectMake(, , , )];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSeperator.width = -;
[self.navigationItem setRightBarButtonItems:@[negativeSeperator, rightBarButtonItem]];
}
else
{
[self.navigationItem setRightBarButtonItem:rightBarButtonItem animated:NO];
} [self addScrollView];
[self addImageViewsToScrollView];
[self addPageControl];
[self setDefaultInfo];
}
else{
UIButton *info = [UIButton buttonWithType:UIButtonTypeCustom];
[info setTitle:@"没有对比截图哦" forState:UIControlStateNormal];
[info setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[info setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[info setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[info setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
info.titleLabel.font = [UIFont systemFontOfSize:];
[info setFrame:CGRectMake(, , kCapPicWidth, )];
[self.view addSubview:info]; UIButton *info2 = [UIButton buttonWithType:UIButtonTypeCustom];
[info2 setTitle:@"请通过商品详情页右上角‘加入对比’添加" forState:UIControlStateNormal];
[info2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[info2 setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
[info2 setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[info2 setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
info2.titleLabel.font = [UIFont systemFontOfSize:];
[info2 setFrame:CGRectMake(, , kCapPicWidth, )];
[self.view addSubview:info2]; }
}
- (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor =[UIColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.00];
// self.view.backgroundColor = [UIColor blackColor];
/*
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 20, 100, 30)];
[backButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[backButton setTitle:[NSString stringWithFormat:@"返回"] forState: UIControlStateNormal];
backButton.backgroundColor = [UIColor clearColor];
[backButton setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont systemFontOfSize: 15.0];
[backButton addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:backButton];
*/
_currentImageIndex = ;
[self initView]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(capPicChanged:) name:@"capPicChanged" object:nil]; }
-(void) capPicChanged:(NSNotification*)notify
{
NSLog(@"picviewcontroller capPicChanged count:%d",[DBStrore sharedDBStore].CapPicArray.count);
for(UIView *view in [self.view subviews])
{
[view removeFromSuperview];
}
[self initView]; } -(void)deletePic
{
NSLog(@"deleteAllPic");
if(_capPicArray.count > ){
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"" message:@"请选择" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * actionAll = [UIAlertAction actionWithTitle:@"删除全部图片" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[self deleteallalert];
NSLog(@"alertController all 被调用"); }];
UIAlertAction * actionCur = [UIAlertAction actionWithTitle:@"删除当前图片" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[self deletecur];
NSLog(@"alertController cur 被调用"); }];
UIAlertAction * actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"alertController调用取消操作");
}]; [alert addAction:actionCur];
[alert addAction:actionAll];
[alert addAction:actionCancel];
[self presentViewController:alert animated:YES completion:nil];
}
else{
//[Utilities showAlertView:@"成功"];
}
}
-(void)deleteallalert
{
NSLog(@"deleteallalert");
if(_capPicArray.count > ){
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"" message:@"确定要删除全部对比图片吗?" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction * actionAll = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
[self deleteall];
NSLog(@"alertController all 被调用"); }];
UIAlertAction * actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"alertController调用取消操作");
}];
[alert addAction:actionAll];
[alert addAction:actionCancel];
[self presentViewController:alert animated:YES completion:nil];
}
else{
//[Utilities showAlertView:@"成功"];
}
} -(void)deleteall
{
NSFileManager *fileManager=[[NSFileManager alloc] init];
NSError *error;
for(int i=;i<_imageCount;i++)
{
NSString *imgpath=[self getPicPathByIndex:i];
NSLog(@"del all imgpath:%@",imgpath);
if([fileManager fileExistsAtPath:imgpath]){
[fileManager removeItemAtPath:imgpath error: &error];
if(error){
NSLog(@"del all file error = %@",error);
}
} }
[[DBStrore sharedDBStore].CapPicArray removeAllObjects];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *bindUserLocalSettingDictionaryData = [NSKeyedArchiver archivedDataWithRootObject:[DBStrore sharedDBStore].CapPicArray];
[defaults setObject:bindUserLocalSettingDictionaryData forKey:FTcapPic];
[defaults synchronize]; for(UIView *view in [self.view subviews])
{
[view removeFromSuperview];
}
[self initView];
//[Utilities showAlertView:@"成功"];
}
-(void)deletecur
{
NSFileManager *fileManager=[[NSFileManager alloc] init];
NSError *error;
for(int i=;i<_imageCount;i++)
{
NSString *imgpath=[self getPicPathByIndex:i];
NSLog(@"del cur imgpath:%@",imgpath);
if(_currentImageIndex==i && [fileManager fileExistsAtPath:imgpath]){
[fileManager removeItemAtPath:imgpath error: &error];
if(error){
NSLog(@"del cur file error = %@",error);
}
break;
} }
[[DBStrore sharedDBStore].CapPicArray removeObjectAtIndex:_currentImageIndex];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *bindUserLocalSettingDictionaryData = [NSKeyedArchiver archivedDataWithRootObject:[DBStrore sharedDBStore].CapPicArray];
[defaults setObject:bindUserLocalSettingDictionaryData forKey:FTcapPic];
[defaults synchronize]; for(UIView *view in [self.view subviews])
{
[view removeFromSuperview];
} [self initView];
//[Utilities showAlertView:@"成功"];
} -(void)backBtnClick:(id)sender
{
NSLog(@"backBtnClick");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self.navigationController popViewControllerAnimated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)loadImageData {
_capPicArray = [[NSMutableArray alloc] initWithArray:[DBStrore sharedDBStore].CapPicArray];
_imageCount = _capPicArray.count;
NSLog(@"loadImageData:count=%d",_capPicArray.count);
} - (void)addScrollView {
// _scrV = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_scrV = [[UIScrollView alloc] initWithFrame:CGRectMake(kCapPicStartx, kScrVStarty, kCapPicWidth, kScrVHeight)];
_scrV.contentSize = CGSizeMake(kCapPicWidth * kImageViewCount, kCapPicHeight);
_scrV.contentOffset = CGPointMake(kCapPicWidth, 0.0);
_scrV.pagingEnabled = YES;
_scrV.showsHorizontalScrollIndicator = NO;
_scrV.delegate = self;
[self.view addSubview:_scrV];
} - (void)addImageViewsToScrollView {
//图片视图;左边
_shopNameLeft = [[UIButton alloc] initWithFrame:CGRectMake(, , kCapPicWidth, kShopNameLabelHeight)];
[_shopNameLeft setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[_shopNameLeft setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
_shopNameLeft.backgroundColor = [UIColor whiteColor];
[_shopNameLeft setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
_shopNameLeft.titleLabel.font = [UIFont systemFontOfSize: 15.0];
[_scrV addSubview:_shopNameLeft]; _goodsURLLeft = [[UIButton alloc] initWithFrame:CGRectMake(kCapPicWidth-kGoodsUrlBtnWidth-, (kShopNameLabelHeight-kGoodsUrlBtnHeight)/, kGoodsUrlBtnWidth, kGoodsUrlBtnHeight)];
[_goodsURLLeft setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[_goodsURLLeft setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
_goodsURLLeft.backgroundColor = [UIColor redColor];
[_goodsURLLeft setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
[_goodsURLLeft setTitle:@"去购买" forState: UIControlStateNormal];
_goodsURLLeft.titleLabel.font = [UIFont systemFontOfSize: 15.0];
_goodsURLLeft.layer.cornerRadius = ;
[_goodsURLLeft addTarget:self action:@selector(toBuyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[_scrV addSubview:_goodsURLLeft]; _imgVLeft = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, kScrVImgStarty, kCapPicWidth, kCapPicHeight)];
_imgVLeft.contentMode = UIViewContentModeScaleAspectFit; _leftpicView = [[UIControl alloc] initWithFrame:CGRectMake(0.0, kScrVImgStarty, kCapPicWidth, kCapPicHeight)] ;
_leftpicView.backgroundColor = [UIColor clearColor];
[(UIControl *)_leftpicView addTarget:self action:@selector(toBuyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[_scrV addSubview:_imgVLeft]; [_scrV addSubview:_leftpicView]; //图片视图;中间
_shopNameCenter = [[UIButton alloc] initWithFrame:CGRectMake(kCapPicWidth, , kCapPicWidth, kShopNameLabelHeight)];
[_shopNameCenter setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[_shopNameCenter setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
_shopNameCenter.backgroundColor = [UIColor whiteColor];
[_shopNameCenter setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
_shopNameCenter.titleLabel.font = [UIFont systemFontOfSize: 15.0];
[_scrV addSubview:_shopNameCenter]; _goodsURLCenter = [[UIButton alloc] initWithFrame:CGRectMake(*kCapPicWidth-kGoodsUrlBtnWidth-, (kShopNameLabelHeight-kGoodsUrlBtnHeight)/, kGoodsUrlBtnWidth, kGoodsUrlBtnHeight)];
[_goodsURLCenter setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[_goodsURLCenter setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
_goodsURLCenter.backgroundColor = [UIColor redColor];
[_goodsURLCenter setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
[_goodsURLCenter setTitle:@"去购买" forState: UIControlStateNormal];
_goodsURLCenter.titleLabel.font = [UIFont systemFontOfSize: 15.0];
_goodsURLCenter.layer.cornerRadius = ;
[_goodsURLCenter addTarget:self action:@selector(toBuyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[_scrV addSubview:_goodsURLCenter]; _imgVCenter = [[UIImageView alloc] initWithFrame:CGRectMake(kCapPicWidth, kScrVImgStarty, kCapPicWidth, kCapPicHeight)];
_imgVCenter.contentMode = UIViewContentModeScaleAspectFit; _centerpicView = [[UIControl alloc] initWithFrame:CGRectMake(kCapPicWidth, kScrVImgStarty, kCapPicWidth, kCapPicHeight)] ;
_centerpicView.backgroundColor = [UIColor clearColor];
[(UIControl *)_centerpicView addTarget:self action:@selector(toBuyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[_scrV addSubview:_imgVCenter]; [_scrV addSubview:_centerpicView]; //图片视图;右边
_shopNameRight = [[UIButton alloc] initWithFrame:CGRectMake(kCapPicWidth * 2.0, , kCapPicWidth, kGoodsUrlBtnHeight)];
[_shopNameRight setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[_shopNameRight setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
_shopNameRight.backgroundColor = [UIColor whiteColor];
[_shopNameRight setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
_shopNameRight.titleLabel.font = [UIFont systemFontOfSize: 15.0];
[_scrV addSubview:_shopNameRight]; _goodsURLRight = [[UIButton alloc] initWithFrame:CGRectMake(*kCapPicWidth-kGoodsUrlBtnWidth-, (kShopNameLabelHeight-kGoodsUrlBtnHeight)/, kGoodsUrlBtnWidth, kGoodsUrlBtnHeight)];
[_goodsURLRight setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[_goodsURLRight setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
_goodsURLRight.backgroundColor = [UIColor redColor];
[_goodsURLRight setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
[_goodsURLRight setTitle:@"去购买" forState: UIControlStateNormal];
_goodsURLRight.titleLabel.font = [UIFont systemFontOfSize: 15.0];
_goodsURLRight.layer.cornerRadius = ;
[_goodsURLRight addTarget:self action:@selector(toBuyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[_scrV addSubview:_goodsURLRight]; _imgVRight = [[UIImageView alloc] initWithFrame:CGRectMake(kCapPicWidth * 2.0, kScrVImgStarty, kCapPicWidth, kCapPicHeight)];
_imgVRight.contentMode = UIViewContentModeScaleAspectFit; _rightpicView = [[UIControl alloc] initWithFrame:CGRectMake(kCapPicWidth * 2.0, kScrVImgStarty, kCapPicWidth, kCapPicHeight)] ;
_rightpicView.backgroundColor = [UIColor clearColor];
[(UIControl *)_rightpicView addTarget:self action:@selector(toBuyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[_scrV addSubview:_imgVRight]; [_scrV addSubview:_rightpicView]; }
- (void)toBuyBtnClick:(id)sender
{
NSLog(@"toBuyBtnClick:%d",_currentImageIndex);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[self getGoodsUrlByIndex:_currentImageIndex] forKey:@"urldata"];
NSString *goodsidstr=[NSString stringWithFormat:@"-1"];
[defaults setObject:goodsidstr forKey:@"goodsiddata"];
NSString *itemidstr=[NSString stringWithFormat:@"-1"];
[defaults setObject:itemidstr forKey:@"itemiddata"];
NSString *shopname=[self getPicShopNameByIndex:_currentImageIndex];
[defaults setObject:shopname forKey:@"shopnamedata"];
[defaults synchronize];
[[DBStrore sharedDBStore] showInformationView:]; }
- (void)addPageControl {
_pageC = [UIPageControl new];
CGSize size= [_pageC sizeForNumberOfPages:_imageCount]; //根据页数返回 UIPageControl 合适的大小
_pageC.bounds = CGRectMake(0.0, 0.0, size.width, size.height);
_pageC.center = CGPointMake(kCapPicWidth / 2.0, kScreenHeight-);
_pageC.numberOfPages = _imageCount;
_pageC.pageIndicatorTintColor = [UIColor lightGrayColor];
_pageC.currentPageIndicatorTintColor = self.view.tintColor;
_pageC.userInteractionEnabled = NO; //设置是否允许用户交互;默认值为 YES,当为 YES 时,针对点击控件区域左(当前页索引减一,最小为0)右(当前页索引加一,最大为总数减一),可以编写 UIControlEventValueChanged 的事件处理方法
UIView *pageview = [[UIView alloc] initWithFrame:CGRectMake(, kScreenHeight-, kCapPicWidth, )];
pageview.alpha=0.90f;
pageview.backgroundColor=[UIColor whiteColor];
[self.view addSubview:pageview]; [self.view addSubview:_pageC];
}
- (NSString*)getPicShopNameByIndex:(NSUInteger)currentImageIndex {
NSString *key=_capPicArray[currentImageIndex];
NSLog(@"cur key:%@",key);
NSArray *tmparr=[_capPicArray[currentImageIndex] componentsSeparatedByString:@","];
NSString *shopname=tmparr[];
NSLog(@"cur shopname:%@",shopname);
return shopname;
}
- (NSString*)getPicPathByIndex:(NSUInteger)currentImageIndex {
NSString *key=_capPicArray[currentImageIndex];
//NSLog(@"cur key:%@",key);
NSArray *tmparr=[_capPicArray[currentImageIndex] componentsSeparatedByString:@","];
NSString *picPath=tmparr[];
//NSLog(@"cur picPath:%@",picPath);
return picPath;
}
- (NSString*)getGoodsUrlByIndex:(NSUInteger)currentImageIndex {
NSString *key=_capPicArray[currentImageIndex];
//NSLog(@"cur key:%@",key);
NSArray *tmparr=[_capPicArray[currentImageIndex] componentsSeparatedByString:@","];
NSString *goodsurl=tmparr[];
// NSLog(@"cur goodsurl:%@",goodsurl);
return goodsurl;
} - (void)setInfoByCurrentImageIndex:(NSUInteger)currentImageIndex {
if(_imageCount>){
_imgVCenter.image = [UIImage imageNamed:[self getPicPathByIndex:currentImageIndex]];
[_shopNameCenter setTitle:[NSString stringWithFormat:@"%@商品截图",[self getPicShopNameByIndex:currentImageIndex]] forState: UIControlStateNormal]; _imgVLeft.image = [UIImage imageNamed:[self getPicPathByIndex:((_currentImageIndex - + _imageCount) % _imageCount)]];
[_shopNameLeft setTitle:[NSString stringWithFormat:@"%@商品截图",[self getPicShopNameByIndex:((_currentImageIndex - + _imageCount) % _imageCount)]] forState: UIControlStateNormal]; _imgVRight.image = [UIImage imageNamed:[self getPicPathByIndex:((_currentImageIndex + ) % _imageCount)]];
[_shopNameRight setTitle:[NSString stringWithFormat:@"%@商品截图",[self getPicShopNameByIndex:((_currentImageIndex + ) % _imageCount)]] forState: UIControlStateNormal]; _pageC.currentPage = currentImageIndex;
}
} - (void)setDefaultInfo {
// _currentImageIndex = 0;
if(_currentImageIndex >= _imageCount){
_currentImageIndex = _imageCount-;
}
[self setInfoByCurrentImageIndex:_currentImageIndex];
} - (void)reloadImage {
CGPoint contentOffset = [_scrV contentOffset];
if (contentOffset.x > kCapPicWidth) { //向左滑动
_currentImageIndex = (_currentImageIndex + ) % _imageCount;
} else if (contentOffset.x < kCapPicWidth) { //向右滑动
_currentImageIndex = (_currentImageIndex - + _imageCount) % _imageCount;
} [self setInfoByCurrentImageIndex:_currentImageIndex];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
//self.navigationController.navigationBarHidden = YES;
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:YES];
//self.navigationController.navigationBarHidden = NO;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self reloadImage]; _scrV.contentOffset = CGPointMake(kCapPicWidth, 0.0);
_pageC.currentPage = _currentImageIndex; } @end

加扣群385378208即可下载工程源码