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

jopen 12年前發布 | 30K 次閱讀 IOS iOS開發 移動開發


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

  教程地址:http://www.baiduhome.net/lib/view/open1341882507072.html

 

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

我們創建一個名為HypnosisView的objetctive-C class文件:

HypnosisView.h:

#import <FOUNDATION foundation.h="">

@interface HypnosisView : UIView {

}

@end</foundation></pre>

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

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

#import <UIKIT uikit.h="">

@class HypnosisView;

@interface HypnosisterAppDelegate : NSObject <UIAPPLICATIONDELEGATE, uiscrollviewdelegate=""> {

HypnosisView *view;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end</UIAPPLICATIONDELEGATE,></uikit></pre>
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</pre>


最后運行一下看下效果:

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

轉自:http://blog.csdn.net/itachi85/article/details/7726992

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!