iOS_基于ScrollView的一个简单相册

原创 2015年11月17日 17:39:20
这个相册是好久之前写的, 其实主要就是用了下ScrollView

#import"AppDelegate.h"
#import
"MainViewController.h"
@implementationAppDelegate
- (
void)dealloc
{
   
//[_window release];
    [
superdealloc];
}

- (
BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
   
self.window= [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
   
// Override point for customization after application launch.
   
self.window.backgroundColor= [UIColorwhiteColor];
   
MainViewController * mc = [[MainViewControlleralloc]init];
   
UINavigationController * NC = [[UINavigationControlleralloc]initWithRootViewController:mc];
   
self.window.rootViewController= NC;
    [
self.windowmakeKeyAndVisible];
   
return YES;
}

~~~~~列表~~~~~
#import<UIKit/UIKit.h>
#import
"PhotoViewController.h"
@interfaceMainViewController :UIViewController

@property(retain,atomic)NSMutableArray * imageArray;
@property(retain,nonatomic)UIScrollView * scrollView;
@property(assign,nonatomic)int            row;
@property(assign,nonatomic)int            numOfPhoto;
@property(retain,nonatomic)PhotoViewController * photoView;
@end


#import"MainViewController.h"

@interfaceMainViewController ()

@end

@implementationMainViewController

- (
id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
   
self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
   
if (self) {
       
// Custom initialization
    }
   
return self;
}

- (
void)viewDidLoad
{
    [
superviewDidLoad];
   
self.automaticallyAdjustsScrollViewInsets= NO;
   
   
self.navigationItem.title= @"相册";
   
// Do any additional setup after loading the view.
   
// 自动添加图片部分
   
_imageArray = [[NSMutableArrayarray]retain];
   
for (intnum = 0 ; num <28 ; num ++) {
       
UIImage * image = [UIImageimageNamed:[NSStringstringWithFormat:@"%03d.jpg",num]];
        [
_imageArrayaddObject:image];
    }
   
_numOfPhoto = _imageArray.count;
   
_row = (_numOfPhoto%4)?1:0;
   
_row +=_numOfPhoto/4;
   
   
_scrollView = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,320,480)];
   
_scrollView.contentSize= CGSizeMake(320,80 *_row);
   
_scrollView.backgroundColor= [UIColorgrayColor];
   
   
for (intnum = 0; num <_numOfPhoto ; num++ ) {
       
UIButton * button = [UIButtonbuttonWithType:UIButtonTypeSystem];
        button.
frame= CGRectMake((num%4)*80, (num/4)*80+ 66,78,78);
       
//button.backgroundColor = [UIColor redColor];
        [button
setBackgroundImage:_imageArray[num]forState:UIControlStateNormal];
        [button
addTarget:selfaction:@selector(enter:)forControlEvents:UIControlEventTouchUpInside];
        [
_scrollViewaddSubview:button];
    }
    [
self.viewaddSubview:_scrollView];
   
    [
_scrollViewrelease];
   
}

- (
void)didReceiveMemoryWarning
{
    [
superdidReceiveMemoryWarning];
   
// Dispose of any resources that can be recreated.
}
- (
void)enter:(UIButton*)button
{
   
CGPoint or = button.frame.origin;
   
int buNum = or.x/80+ (or.y- 66)/80* 4;
   
NSLog(@"%d",buNum);
   
   
_photoView = [[PhotoViewControlleralloc]init];
   
_photoView.current= buNum;
    [
_photoViewsetImageArray:_imageArray];
    [
self.navigationControllerpushViewController:_photoViewanimated:YES];
   
}

~~~~~ 相册 ~~~~~
#import<UIKit/UIKit.h>

@interfacePhotoViewController :UIViewController
@property(assign,nonatomic)int              current;
@property(assign,nonatomic)int              numOfPhoto;
@property(retain,nonatomic)NSMutableArray  *imageArray;
@property(retain,nonatomic)UIScrollView    *scrollview;
@end


#import"PhotoViewController.h"

@interfacePhotoViewController ()

@end

@implementationPhotoViewController

- (
id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
   
self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
   
if (self) {
       
// Custom initialization
    }
   
return self;
}

- (
void)viewDidLoad
{
    [
superviewDidLoad];
   
self.automaticallyAdjustsScrollViewInsets= NO;
   
// Do any additional setup after loading the view.
   
self.view.backgroundColor= [UIColorwhiteColor];
   
_numOfPhoto = [_imageArraycount];
   
self.navigationItem.title= [NSStringstringWithFormat:@"%d/%d",_current+1,_numOfPhoto];
   
   
_scrollview = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,320,480)];
   
_scrollview.contentSize= CGSizeMake(320*_numOfPhoto,480);
   
_scrollview.pagingEnabled= YES;
   
_scrollview.delegate= self;
    [
_scrollviewsetContentOffset:CGPointMake(_current* 320,0)animated:NO];
   
   
for (intnum = 0;num <_numOfPhoto;num++) {
       
UIScrollView * scroll = [[UIScrollViewalloc]initWithFrame:CGRectMake(320* num,0,320,480)];
       
UIImageView * ima = [[UIImageViewalloc]initWithImage:_imageArray[num] ];
        ima.
frame= CGRectMake(0,0,320,480);
        [scroll
addSubview:ima];
        scroll.
minimumZoomScale= 0.4;
        scroll.
maximumZoomScale= 3;
        scroll.
delegate= self;
        [
_scrollviewaddSubview: scroll];
    }
    [
self.viewaddSubview:_scrollview];
}

- (
void)didReceiveMemoryWarning
{
    [
superdidReceiveMemoryWarning];
   
   
// Dispose of any resources that can be recreated.
}
- (
void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
   
   
int num = (int)_scrollview.contentOffset.x/320;
   
if ( (num -1) >=0) {
       
UIScrollView * sv = [[_scrollviewsubviews]objectAtIndex:(num-1)];
        sv.
zoomScale= 1.0;
       
UIImageView * imv = [[sv subviews]objectAtIndex:0];
        imv.
center= CGPointMake(160,240);
    }
   
if ( (num +1) <3) {
       
UIScrollView * sv = [[_scrollviewsubviews]objectAtIndex:(num+1)];
        sv.
zoomScale= 1.0;
       
//sv.center = CGPointMake(160, 240);
       
UIImageView * imv = [[sv subviews]objectAtIndex:0];
        imv.
center= CGPointMake(160,240);
    }
}
- (
void)scrollViewDidZoom:(UIScrollView*)scrollView
{
   
NSLog(@"%s",__FUNCTION__);
   
UIImageView * iv = [scrollView.subviewsobjectAtIndex:0];
   
CGSize size = iv.frame.size;
   
if (size.width< 320)
        [[scrollView.
subviewsobjectAtIndex:0]setCenter:CGPointMake(160,240)];
}
- (
void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
{
   
int num = (_scrollview.contentOffset.x/320);
   
_current = num;
   
self.navigationItem.title= [NSStringstringWithFormat:@"%d/%d",_current+1,_numOfPhoto];
   
}
- (
UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
{
   
return [scrollView.subviewsobjectAtIndex:0];
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

【iOS】一个完整的简单的调用系统相机和相册设置头像

1.Xcode8,iOS10的权限设置(不设置会崩溃): 找到项目的info.plist文件,右键open As,以Source Code的形式打开,将以下代码添加进去: 相机权限设置: NSCame...

IOS scrollview做的相册

  • 2014年11月21日 18:22
  • 194KB
  • 下载

iOS 无线滚动相册之scrollView

@implementation RootViewController - (void)dealloc { [_scrollView release]; [_pageControl re...

iOS scrollView无限循环的简单实现

  • 2016年03月30日 11:05
  • 381KB
  • 下载

iOS最基本 最简单的相册

  • 2015年11月04日 11:43
  • 807KB
  • 下载

Web---自己写的一个简单云相册~

实现的功能是: 用户可以一次上传一个至多个文件。 用户可以下载其他人上传的图片。 用户可以查看其他所有人的图片。 用户只能删除通过自己IP上传的图片。用到的技术: 文件上传下载、设计模式、D...

用AJAX编写一个简单的相册

  • 2010年02月23日 10:25
  • 3KB
  • 下载

一个简单的相册

  • 2014年04月01日 17:24
  • 3.17MB
  • 下载

Android:横向ListView(HorizontalListView)及一个简单相册的完整实现 (附源码下载)

本文内容: 1、横向ListView的所有实现思路; 2、其中一个最通用的思路HorizontalListView,并基于横向ListView开发一个简单的相册; 3、实现的横向ListView...

简单的一个相册

  • 2017年05月31日 16:18
  • 4KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS_基于ScrollView的一个简单相册
举报原因:
原因补充:

(最多只允许输入30个字)