views:

207

answers:

1

Hi,

I want to publish tutorial about how to easily create an UIScrollView on http://www.xprogress.com/ and I just want to check with you guys if the code is alright before I publish anything. Any help will be appreciated and I'll put your name / website on the bottom of the article :)

Thanks a lot :)

Ondrej

header file:

///
///  IGUIScrollViewImage.h
///
///  IGUILibrary
///
///  Created by Ondrej Rafaj on 7.4.10.
///
///  Copyright 2010 Home. All rights reserved.
///
/// @todo enable margin and center the image to the middle of the view

/**

 <b>Examples:</b>

 <i>This is just a short example how to use this class</i>

 <pre>
 - (NSArray *)getImages {
    NSMutableArray *arr = [[[NSMutableArray alloc] init] autorelease];
    [arr addObject:[UIImage imageNamed:@"image-1.jpg"]];
    [arr addObject:[UIImage imageNamed:@"image-2.png"]];
    [arr addObject:[UIImage imageNamed:@"image-3.png"]];
    [arr addObject:[UIImage imageNamed:@"image-4.jpg"]];
    return (NSArray *)arr;
 }

 - (void)viewDidLoad {
    IGUIScrollViewImage *svimage = [[IGUIScrollViewImage alloc] init];

    [svimage setSizeFromScrollView:self.scrView]; // takes size of the scroll view you've already placed on stage via Interface Builder
    // or
    //[svimage setWidth:320 andHeight:240]; // half screen

    [svimage enablePositionMemory]; // enables position (pagination) memory for this scroll view
    // or
    //[svimage enablePositionMemoryWithIdentifier:@"myIdentifier"]; if you have more instances of this scroll view in your application

    [svimage enablePageControlOnBottom];
    // or
    //[svimage enablePageControlOnTop];

    [self.myUIView addSubview:[svimage get]]; // and place it on the stage :)
    [super viewDidLoad];
 }
 </pre>


 */

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>


@interface IGUIScrollViewImage : NSObject <UIScrollViewDelegate> {

    UIScrollView *scrollView;
    UIPageControl *pageControl;

    CGRect rectScrollView;
    CGRect rectPageControl;

    int scrollWidth;
    int scrollHeight;

    NSArray *contentArray;

    UIColor *bcgColor;

    BOOL pageControlEnabledTop;
    BOOL pageControlEnabledBottom;

    BOOL rememberPosition;
    NSString *positionIdentifier;

}

@property (nonatomic, retain) UIScrollView *scrollView;

- (int)getScrollViewWidth;

- (void)setWidth:(int)width andHeight:(int)height;

- (void)setSizeFromScrollView:(UIScrollView *)scView;

- (void)setBackGroudColor:(UIColor *)color;

- (void)setContentArray:(NSArray *)images;

- (void)enablePageControlOnTop;

- (void)enablePageControlOnBottom;

- (void)enablePositionMemory;

- (void)enablePositionMemoryWithIdentifier:(NSString *)identifier;

- (UIScrollView *)getWithPosition:(int)page;

- (UIScrollView *)getWithPositionMemoryIdentifier:(NSString *)identifier;

- (UIScrollView *)get;

@end

And the implementation file:

//
//  IGUIScrollViewImage.m
//  IGUILibrary
//
//  Created by Ondrej Rafaj on 7.4.10.
//  Copyright 2010 Home. All rights reserved.
//

#import "IGUIScrollViewImage.h"

#define kIGUIScrollViewImagePageIdentifier                      @"kIGUIScrollViewImagePageIdentifier"
#define kIGUIScrollViewImageDefaultPageIdentifier               @"Default"


@implementation IGUIScrollViewImage

@synthesize scrollView;

- (int)getScrollViewWidth {
    return ([contentArray count] * scrollWidth);
}

- (void)setWidth:(int)width andHeight:(int)height {
    scrollWidth = width;
    scrollHeight = height;
    if (!width || !height) rectScrollView = [[UIScreen mainScreen] applicationFrame];
    else rectScrollView = CGRectMake(0, 0, width, height);
}

- (void)setSizeFromScrollView:(UIScrollView *)scView {
    scrollWidth = scView.frame.size.width;
    scrollHeight = scView.frame.size.height;
    rectScrollView = CGRectMake(0, 0, scrollWidth, scrollHeight);
}

- (void)setContentArray:(NSArray *)images {
    contentArray = images;
}

- (void)setBackGroudColor:(UIColor *)color {
    bcgColor = color;
}

- (void)enablePageControlOnTop {
    pageControlEnabledTop = YES;
}

- (void)enablePageControlOnBottom {
    pageControlEnabledBottom = YES;
}

- (void)enablePositionMemoryWithIdentifier:(NSString *)identifier {
    rememberPosition = YES;
    if (!identifier) identifier = kIGUIScrollViewImageDefaultPageIdentifier;
    positionIdentifier = identifier;
}

- (void)enablePositionMemory {
    [self enablePositionMemoryWithIdentifier:nil];
}

- (UIScrollView *)getWithPosition:(int)page {
    if (!contentArray) {
        contentArray = [[[NSArray alloc] init] autorelease];
    }
    if (page > [contentArray count]) page = 0;

    if (!scrollWidth || !scrollHeight) {
        rectScrollView = [[UIScreen mainScreen] applicationFrame];
        scrollWidth = rectScrollView.size.width;
        scrollHeight = rectScrollView.size.height;
    }
    rectScrollView = CGRectMake(0, 0, scrollWidth, scrollHeight);

    self.scrollView = [[UIScrollView alloc] initWithFrame:rectScrollView];
    self.scrollView.contentSize = CGSizeMake([self getScrollViewWidth], scrollHeight);
    if (!bcgColor) bcgColor = [UIColor blackColor];
    self.scrollView.backgroundColor = bcgColor;
    self.scrollView.alwaysBounceHorizontal = YES;
    self.scrollView.contentOffset = CGPointMake(page * scrollWidth, 0);
    self.scrollView.pagingEnabled = YES;

    UIImageView *imageView;
    UIView *main = [[[UIView alloc] initWithFrame:rectScrollView] autorelease];
    int i = 0;
    for (UIImage *img in contentArray) {
        imageView = [[UIImageView alloc] initWithImage:img];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        imageView.backgroundColor = [UIColor blackColor];
        float ratio = img.size.width/rectScrollView.size.width;
        CGRect imageFrame = CGRectMake(i, 0, rectScrollView.size.width, (img.size.height / ratio));
        imageView.frame = imageFrame;
        [self.scrollView addSubview:imageView];
        i += scrollWidth;
    }
    [imageView release];
    [main addSubview:scrollView];
    if (pageControlEnabledTop) {
        rectPageControl = CGRectMake(0, 5, scrollWidth, 15);
    }
    else if (pageControlEnabledBottom) {
        rectPageControl = CGRectMake(0, (scrollHeight - 25), scrollWidth, 15);
    }
    if (pageControlEnabledTop || pageControlEnabledBottom) {
        pageControl = [[[UIPageControl alloc] initWithFrame:rectPageControl] autorelease];
        pageControl.numberOfPages = [contentArray count];
        pageControl.currentPage = page;
        [main addSubview:pageControl];
    }
    if (pageControlEnabledTop || pageControlEnabledBottom || rememberPosition) self.scrollView.delegate = self;
    //if (margin) [margin release];
    return (UIScrollView *)main;
}

- (UIScrollView *)get {
    return [self getWithPosition:0];
}

- (UIScrollView *)getWithPositionMemoryIdentifier:(NSString *)identifier {
    [self enablePositionMemoryWithIdentifier:identifier];
    return [self getWithPosition:[[[NSUserDefaults alloc] objectForKey:[NSString stringWithFormat:@"%@%@", kIGUIScrollViewImagePageIdentifier, positionIdentifier]] intValue]];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sv {
    int page = sv.contentOffset.x / sv.frame.size.width;
    pageControl.currentPage = page;
    if (rememberPosition) {
        [[NSUserDefaults alloc] setObject:[NSString stringWithFormat:@"%d", page] forKey:[NSString stringWithFormat:@"%@%@", kIGUIScrollViewImagePageIdentifier, positionIdentifier]];
    }
}

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


@end
+1  A: 

Just a quick glance. You alloc UIImageView multiple times in -(UIScrollView*)getWithPosition:(int)page and release it only once:

for (UIImage *img in contentArray) {
    imageView = [[UIImageView alloc] initWithImage:img];
    // ...
}
[imageView release];
Daniel Bauke
uups, you're right, thanks :) ... anything else?
Ondrej