2010-04-09 15 views
1

我要發佈關於如何輕鬆http://www.xprogress.com/創建一個UIScrollView的教程,我只是想檢查與你們如果代碼是好的之前,我發表任何東西。任何幫助將不勝感激,我會把你的名字/網站上的文章:)底部泄漏我的UIScrollView輔助類

感謝很多:)

的Ondrej

頭文件:

/// 
/// 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 

並執行文件:

// 
// 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

只是看一眼。您在頁頭 -(UIScrollView*)getWithPosition:(int)page的UIImageView多次,釋放它只有一次:

for (UIImage *img in contentArray) { 
    imageView = [[UIImageView alloc] initWithImage:img]; 
    // ... 
} 
[imageView release]; 
+0

UUPS,你說得對,謝謝:) ......什麼事嗎? – Ondrej 2010-04-09 14:11:17