开发者

photo gallery uiscrollView scrolling and zooming

HI i'm tring to create photogallery with two scrollviews one for horizontal scrolling between images and second for zooming images.

scrolling between images works fine but i'm not able to zoom image into second scrollView.

mainScrollView named VcLicences

VcLicences.h

#import <UIKit/UIKit.h>


@interface VcLicences : UIViewController <UIScrollViewDelegate>{
    IBOutlet UIScrollView* scrollView;
IBOutlet UIPageControl* pageControl;

    BOOL pageControlIsChangingPage;
}

@property (nonatomic, retain) UIView *scrollView;
@property (nonatomic, retain) UIPageControl* pageControl;

/* for pageControl */
- (IBAction)changePage:(id)sender;

/* internal */
- (void)setupPage;

@end

VcLicences.m

#import "VcLicences.h"
#import "VcImageZoomView.h"


@implementation VcLicences

@synthesize scrollView;
@synthesize pageControl;



- (void)setupPage
{
    scrollView.delegate = self;

    [self.scrollView setBackgroundColor:[UIColor whiteColor]];

    scrollView.indicatorStyle = UIScrollViewIndicatorStyleBlack;
    scrollView.clipsToBounds = YES;
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = YES;
    scrollView.multipleTouchEnabled = NO;


    NSUInteger nim = 0;
    CGFloat cx = 0;

    NSArray *licence = [NSArray arrayWithObjects:@"zl1.开发者_C百科jpg", @"zl2.jpg", @"zl3.jpg", @"dph.jpg", @"or.jpg", nil];

    for (nim=0; nim < [licence count]; nim++) 
    {
        VcImageZoomView *imageZoom = [[VcImageZoomView alloc] initWithNibName:@"VcImageZoomView" bundle:nil];

        [imageZoom.view setFrame:CGRectMake(cx, 0, 320, 332)];

        [imageZoom setMyImage:[licence objectAtIndex:nim]];

        [scrollView addSubview:imageZoom.view];

        [imageZoom release];

        cx += scrollView.frame.size.width;
    }

    self.pageControl.numberOfPages = nim;

    [scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}

#pragma mark UIScrollViewDelegate stuff
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
    if (pageControlIsChangingPage) 
    {
        return;
    }

    CGFloat pageWidth = _scrollView.frame.size.width;
    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    pageControl.currentPage = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView 
{
    pageControlIsChangingPage = NO;
}

- (IBAction)changePage:(id)sender 
{
    CGRect frame = scrollView.frame;
    frame.origin.x = frame.size.width * pageControl.currentPage;
    frame.origin.y = 0;

    [scrollView scrollRectToVisible:frame animated:YES];

    pageControlIsChangingPage = YES;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    self.navigationItem.title = @"CZECHMAT's licenses";
    [self setupPage];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload 
{
    //[scrollView release];
    //[pageControl release];
}

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

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}

@end

second ScrollView for zooming images named VcImageZoomView

VcImageZoomView.h

#import <UIKit/UIKit.h>

@interface VcImageZoomView : UIViewController  <UIScrollViewDelegate>{
    IBOutlet UIScrollView *scroll;
    UIImageView *image;
}

@property (nonatomic, retain) UIScrollView *scroll;

-(void)setMyImage:(NSString *) imageName;


@end

VcImageZoomView.m

#import "VcImageZoomView.h"

@implementation VcImageZoomView

@synthesize  scroll;

-(void)setMyImage:(NSString *) imageName;
{
    NSLog(@"%@", imageName);
    image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];

    float scaleWidth = scroll.frame.size.width / image.frame.size.width;
    float scaleHeight = scroll.frame.size.height / image.frame.size.height;

    float scale = scaleWidth < scaleHeight ? scaleWidth : scaleHeight;


    NSLog(@"%f", scale);

    [scroll addSubview:image];
    scroll.minimumZoomScale = scale;
    scroll.maximumZoomScale = 1.0;
    scroll.delegate = self;

    [scroll setZoomScale:scroll.minimumZoomScale];

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{    
    [super viewDidLoad];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return image;
}

@end

thx for way


You really need to look at Apple's PhotoViewer sample and the WWDC'10 and '11 videos about ScrollViews: they are explaining in details (with this very example of a photoviewer scrolling and zooming) how to implement it:

  • How to recycle memory so that your memory footprint is drastically reduced (very important for fluidity)

  • How to handle both scrolling in one view and zooming in the other

  • If needed, how to do image tiling to optimize memory when zooming (only useful if you tiling is precomputed, so if you have images in your bundle anyway)

This is very interesting tutorials and videos and really worth reading/viewing.


https://github.com/doubleencore/KTPhotoBrowser

find above attached link get source code for photo gallery .....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜