日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS开发之自定义View

發布時間:2025/3/21 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发之自定义View 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?iOS sdk中的view是UIView,我們可以很方便的自定義一個View。

創建一個 Window-based Application程序,在其中添加一個Hypnosister的類,這個類選擇繼承UIObject。修改這個類,使他繼承:UIView

@interface HypnosisView :?UIView

自定義View的關鍵是定義drawRect: 方法,因為主要是通過重載這個方法,來改變view的外觀。例如,可以使用下面代碼繪制一個很多環中環的效果的view。

View Code - (void)drawRect:(CGRect)rect
{
// What rectangle am I filling?
CGRect bounds = [self bounds];
// Where is its center?
CGPoint center;
center.x
= bounds.origin.x + bounds.size.width / 2.0;
center.y
= bounds.origin.y + bounds.size.height / 2.0;
// From the center how far out to a corner?
float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0;
// Get the context being drawn upon
CGContextRef context = UIGraphicsGetCurrentContext();
// All lines will be drawn 10 points wide
CGContextSetLineWidth(context, 10);
// Set the stroke color to light gray
[[UIColor lightGrayColor] setStroke];
// Draw concentric circles from the outside in
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20)
{
CGContextAddArc(context, center.x, center.y,
currentRadius,
0.0, M_PI * 2.0, YES);
CGContextStrokePath(context);
}
}

這樣view的效果如下圖:

我們可以繼續繪制一些東西,比如繪制文字,將下面代碼添加帶這個方法后面。

// Create a string
NSString *text = @"我是朱祁林,不是朱麒麟";
// Get a font to draw it in
UIFont *font = [UIFont boldSystemFontOfSize:28];
// Where am I going to draw it?
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;
// Set the fill color of the current context to black
[[UIColor blackColor] setFill];
// Set the shadow to be offset 4 points right, 3 points down,
// dark gray and with a blur radius of 2 points
CGSize offset = CGSizeMake(4, 3);
CGColorRef color
= [[UIColor darkGrayColor] CGColor];
CGContextSetShadowWithColor(context, offset,
2.0, color);
// Draw the string
[text drawInRect:textRect
withFont:font];

效果:

如果view過大,我們可以把它放置到一個UIScrollView中間,這樣就可以進行拖動了。UIScrollView與View的關系如下圖:

使用下面代碼創建一個比iPhone屏幕大4倍的View,然后通過UIScrollView來展示,代碼如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


//創建一個窗體大小的CGRect
CGRect wholeWindow = [[self window] bounds];

// 創建一個窗體大小的HypnosisView實例
view = [[HypnosisView alloc] initWithFrame:wholeWindow];

UIScrollView
*scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];
[[self window] addSubview:scrollView];
// Make your view twice as large as the window
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];
CGPoint offset;
offset.x
= wholeWindow.size.width * 0.5;
offset.y
= wholeWindow.size.height * 0.5;
[scrollView setContentOffset:offset];

// Create the view
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;


}

這樣我們就可以拖動來展示看不到的view了,如下圖:

通過UIScrollView我們還可以設置view的縮放功能,將下面代碼添加到中。這樣我們就可以使用兩根手指縮放view了。

// Enable zooming
[scrollView setMinimumZoomScale:0.5];
[scrollView setMaximumZoomScale:
5];
[scrollView setDelegate:self];

總結:文本簡單的總結了一下自定義view的使用。

代碼:Hypnosister.zip

轉載于:https://blog.51cto.com/no001/641282

總結

以上是生活随笔為你收集整理的iOS开发之自定义View的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。