ios app: view: 怎样创建自己的view
2012-04-19 22:52
239 查看
我是个不喜欢普通的人,Cocao 的common view 设计的很好。也许是审美疲劳,从开始准备写App时我就准备自己写view,让一切看起来与众不同。我也不想分享什么,也不是想帮助任何人。我只是纪录下我学的帮助自己理解,理清自己的思路。
什么解释都不如代码来的快:
init一个view的时候通常需要创建一个frame给它,它以后的范围就是这个frame了。这里是个很典型的view 的init方法。
dealloc 是需要的,release 你创建的东西,每人想要memory leak。
最重要的是-(view)drawRect: (CGRect) rect {...}, 重写这个方法实现自己view的look
Go there:好吧解释一下我的理解:
第一步时我们给自己的view在init建立一个frame就框起来我们view的大小。在drawRect方法里面我们开始画我们的view,当然范围就是我们第一步实现的frame范围里面。然后,我们不希望自己的view时不会动的,所以要提供一个改变的方法给其他人调用,这就是第四步时创建的setupNextDispalyString方法。记住改变了后不会立即成效显现出来。最后要调用一个“[self setNeedsDisplay]”.当绘图周期到的时候,图像就被改变了,很快,快的几乎不让你感觉到。
什么解释都不如代码来的快:
init一个view的时候通常需要创建一个frame给它,它以后的范围就是这个frame了。这里是个很典型的view 的init方法。
- (id)init { // Retrieve the image for the view and determine its size UIImage *image = [UIImage imageNamed:@"Placard.png"]; CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height); // Set self's frame to encompass the image,根据image的size来建立frame并初始化自己。 self = [self initWithFrame:frame]; if (self) { self.opaque = NO; placardImage = image; // Load the display strings NSString *path = [[NSBundle mainBundle] pathForResource:@"DisplayStrings" ofType:@"txt"]; NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF16BigEndianStringEncoding error:NULL]; self.displayStrings = [string componentsSeparatedByString:@"\n"]; displayStringsIndex = 0; [self setupNextDisplayString]; } return self; }
dealloc 是需要的,release 你创建的东西,每人想要memory leak。
- (void)dealloc { [placardImage release]; [currentDisplayString release]; [displayStrings release]; [super dealloc]; }
最重要的是-(view)drawRect: (CGRect) rect {...}, 重写这个方法实现自己view的look
- (void)drawRect:(CGRect)rect { // Draw the placard at 0, 0 [placardImage drawAtPoint:(CGPointMake(0.0f, 0.0f))]; /* Draw the current display string. Typically you would use a UILabel, but this example serves to illustrate the UIKit extensions to NSString. The text is drawn center of the view twice - first slightly offset in black, then in white -- to give an embossed appearance. The size of the font and text are calculated in setupNextDisplayString. */ // Find point at which to draw the string so it will be in the center of the view CGFloat x = self.bounds.size.width/2 - textSize.width/2; CGFloat y = self.bounds.size.height/2 - textSize.height/2; CGPoint point; // Get the font of the appropriate size UIFont *font = [UIFont systemFontOfSize:fontSize]; [[UIColor blackColor] set]; point = CGPointMake(x, y + 0.5f); [currentDisplayString drawAtPoint:point forWidth:(self.bounds.size.width-STRING_INDENT) withFont:font fontSize:fontSize lineBreakMode:UILineBreakModeMiddleTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines]; [[UIColor whiteColor] set]; point = CGPointMake(x, y); [currentDisplayString drawAtPoint:point forWidth:(self.bounds.size.width-STRING_INDENT) withFont:font fontSize:fontSize lineBreakMode:UILineBreakModeMiddleTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines]; }4. 提供方法改变你的view的look当和用户交换的时候
- (void)setupNextDisplayString { // Get the string at the current index, then increment the index self.currentDisplayString = [displayStrings objectAtIndex:displayStringsIndex]; //self.currentDisplayString = @"Mac KLV"; displayStringsIndex++; if (displayStringsIndex >= [displayStrings count]) { displayStringsIndex = 0; } UIFont *font = [UIFont systemFontOfSize:24]; // Precalculate size of text and size of font so that text fits inside placard textSize = [currentDisplayString sizeWithFont:font minFontSize:9.0f actualFontSize:&fontSize forWidth:(self.bounds.size.width-STRING_INDENT) lineBreakMode:UILineBreakModeMiddleTruncation]; [self setNeedsDisplay]; }
Go there:好吧解释一下我的理解:
第一步时我们给自己的view在init建立一个frame就框起来我们view的大小。在drawRect方法里面我们开始画我们的view,当然范围就是我们第一步实现的frame范围里面。然后,我们不希望自己的view时不会动的,所以要提供一个改变的方法给其他人调用,这就是第四步时创建的setupNextDispalyString方法。记住改变了后不会立即成效显现出来。最后要调用一个“[self setNeedsDisplay]”.当绘图周期到的时候,图像就被改变了,很快,快的几乎不让你感觉到。
相关文章推荐
- iOS中在系统相册中创建自己App的自定义相册
- 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 2 部分
- 怎样让玩家在应用内给自己的iOS app评分
- 在iOS中怎样创建可展开的Table View?(上)
- 在iOS中怎样创建可展开的Table View?(下)
- 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 1 部分
- 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 2 部分
- 在iOS中怎样创建可展开的Table View?(上)
- 用 JSQMessagesViewController 创建一个 iOS 聊天 App - 第 3 部分
- 在iOS中怎样创建可展开的Table View?(下)
- iOS:怎样创建一个好的App目录结构
- 详解iOS App中UITableView的创建与内容刷新
- IOS之怎样把自己开发的App安装到越狱的手机
- iOS 创建app外部链接
- 在OpenframeWorks for iOS app 创建文件然后到iTunes里面获得
- iOS怎样找到自己的沙盒
- iOS开发UI篇—控制器的View的创建
- (iPhone/iPad开发)怎样判断ios app 第一次启动
- 用XMPP协议来创建自己的即时聊天app
- iOS -- 给自己的APP加上启动图