ios學習筆記(六)使用UIScrollView嵌套UIView子類

來源:互聯網
上載者:User

      完成這一功能的前提是,你應該先安裝好我上一節所說道的window-Based Application模版:

  教程地址:    http://blog.csdn.net/itachi85/article/details/7706549

接著我們要建立一個window-Based Application 模版

我們建立一個名為HypnosisView的objetctive-C class檔案:

HypnosisView.h:

#import <Foundation/Foundation.h>@interface HypnosisView : UIView{    }@end

HypnosisView.m:

在這個檔案中我們加入了繪製了一個同心圓的圖形

#import "HypnosisView.h"@implementation HypnosisView- (void)drawRect:(CGRect)rect{    // 擷取繪圖區域    CGRect bounds = [self bounds];        // 計算中心點    CGPoint center;    center.x = bounds.origin.x + bounds.size.width / 2.0;    center.y = bounds.origin.y + bounds.size.height / 2.0;        // 計算中心點至邊界角的距離    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;        // 擷取繪圖所需要的上下文    CGContextRef context = UIGraphicsGetCurrentContext();        // 用10點的寬度來繪製所有的線條    CGContextSetLineWidth(context, 10);        // 線條顏色設為淡灰    [[UIColor lightGrayColor] setStroke];    // 繪製同心圓    for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20)    {        CGContextAddArc(context, center.x, center.y,                         currentRadius, 0.0, M_PI * 2.0, YES);        CGContextStrokePath(context);    }           NSString *text = @"hi: i am henrymorgen.";        // 擷取繪圖所需要的字型    UIFont *font = [UIFont boldSystemFontOfSize:28];        // 計算繪圖位置    CGRect textRect;    textRect.size = [text sizeWithFont:font];    textRect.origin.x = center.x - textRect.size.width / 2.0;    textRect.origin.y = center.y - textRect.size.height / 2.0;        // 將當前內容相關的填充色設為黑色    [[UIColor blackColor] setFill];    [text drawInRect:textRect            withFont:font];}@end

接著我們配置代理 AppDelegate.h:

#import <UIKit/UIKit.h>@class HypnosisView;@interface HypnosisterAppDelegate : NSObject    <UIApplicationDelegate, UIScrollViewDelegate> {        HypnosisView *view;}@property (nonatomic, retain) IBOutlet UIWindow *window;@end

AppDelegate.m:

在代理中我們添加了ScrollView,並將先前的HypnosisView添加到ScrollView中

#import "HypnosisterAppDelegate.h"#import "HypnosisView.h"@implementation HypnosisterAppDelegate@synthesize window=_window;- (BOOL)application:(UIApplication *)application    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    CGRect wholeWindow = [[self window] bounds];        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];    [[self window] addSubview:scrollView];        // 將視圖的大小設為視窗的二倍    CGRect reallyBigRect;    reallyBigRect.origin = CGPointZero;    reallyBigRect.size.width = wholeWindow.size.width * 2.0;    reallyBigRect.size.height = wholeWindow.size.height * 2.0;    [scrollView setContentSize:reallyBigRect.size];        // 在UIScrollView對象裡置中    CGPoint offset;    offset.x = wholeWindow.size.width * 0.5;    offset.y = wholeWindow.size.height * 0.5;    [scrollView setContentOffset:offset];        // 啟用縮放功能    [scrollView setMinimumZoomScale:0.5];    [scrollView setMaximumZoomScale:5];    [scrollView setDelegate:self];        // 建立視圖    view = [[HypnosisView alloc] initWithFrame:reallyBigRect];    [view setBackgroundColor:[UIColor clearColor]];    [scrollView addSubview:view];        [scrollView release];            [[UIApplication sharedApplication] setStatusBarHidden:YES                                    withAnimation:UIStatusBarAnimationFade];        [[self window] makeKeyAndVisible];    return YES;}- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{    return view;}- (void)dealloc{    [view release];    [_window release];    [super dealloc];}@end

最後運行一下看下效果:


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.