Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 717543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:25:30+00:00 2026-05-14T05:25:30+00:00

I want to publish tutorial about how to easily create an UIScrollView on http://www.xprogress.com/

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:25:30+00:00Added an answer on May 14, 2026 at 5:25 am

    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];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.