关闭

iOS_基于ScrollView的一个简单相册

149人阅读 评论(0) 收藏 举报
这个相册是好久之前写的, 其实主要就是用了下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];
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5804次
    • 积分:339
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:11篇
    • 译文:0篇
    • 评论:0条
    文章分类