本文主要是介绍UIScrollView 编程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
再 ios 编程中 UIScrollView 占有非常重要的位置,当要显示的内容超出屏幕的大小时,就需要使用UIScrollView来进行滚动显示,程序事先设置了三张图片,这三张图片的大小远远超过了屏幕的大小远远超过了屏幕的物理尺寸,所以使用一个UIScrollView来装载图片的内容代码如下
这是ScrollView.h的代码
#import <UIKit/UIKit.h>@interface ScrollAppDelegate : NSObject <UIApplicationDelegate, UIScrollViewDelegate> {UIWindow *window;UIScrollView *scrollView;UIView *containerView;
}@property (nonatomic, retain) IBOutlet UIWindow *window;@end
这是ScrollView.m的代码:
#import "ScrollAppDelegate.h"@implementation ScrollAppDelegate@synthesize window;- (void)applicationDidFinishLaunching:(UIApplication *)application { // Create a scroll viewscrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];scrollView.delegate = self;scrollView.bouncesZoom = YES;scrollView.backgroundColor = [UIColor blackColor];// Create a container view. We need to return this in -viewForZoomingInScrollView: below.containerView = [[UIView alloc] initWithFrame:CGRectZero];[scrollView addSubview:containerView];// Add image views for each of our imagesCGFloat maximumWidth = 0.0;CGFloat totalHeight = 0.0;for (int i = 1; i <= 3; i++) {UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]];CGRect frame = CGRectMake(0, totalHeight, image.size.width, image.size.height);UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];imageView.image = image;[containerView addSubview:imageView];[imageView release];// Increment our maximum width & total heightmaximumWidth = MAX(maximumWidth, image.size.width);totalHeight += image.size.height;}// Size the container view to fit. Use its size for the scroll view's content size as well.containerView.frame = CGRectMake(0, 0, maximumWidth, totalHeight);scrollView.contentSize = containerView.frame.size;// Minimum and maximum zoom scalesscrollView.minimumZoomScale = scrollView.frame.size.width / maximumWidth;scrollView.maximumZoomScale = 2.0;[window addSubview:scrollView];[window makeKeyAndVisible];
}- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{return containerView;
}- (void)dealloc {[scrollView release];[containerView release];[window release];[super dealloc];
}
这篇关于UIScrollView 编程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!