ios app: view: 怎样创建自己的view
来源:互联网 发布:不亦…乎 编辑:程序博客网 时间:2024/05/01 16:40
我是个不喜欢普通的人,Cocao 的common view 设计的很好。也许是审美疲劳,从开始准备写App时我就准备自己写view,让一切看起来与众不同。我也不想分享什么,也不是想帮助任何人。我只是纪录下我学的帮助自己理解,理清自己的思路。
什么解释都不如代码来的快:
- init一个view的时候通常需要创建一个frame给它,它以后的范围就是这个frame了。这里是个很典型的view 的init方法。
- (id)init {// Retrieve the image for the view and determine its sizeUIImage *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 stringsNSString *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 viewCGFloat 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 sizeUIFont *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 indexself.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 placardtextSize = [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: view: 怎样创建自己的view
- 怎样创建 View Only 的 Form
- ios view的创建过程
- 自定义view学习-创建自己的九宫格解锁view
- 在iOS中怎样创建可展开的Table View?(上)
- 在iOS中怎样创建可展开的Table View?(下)
- 自己创建View和改变View
- iOS中不同view的创建方法
- iOS开发之控制器View的创建
- [IOS]创建控制器View的优先级关系
- iOS 自定义view 如何不使用self.view也就是系统自带的 view,使用自己定制的view
- 定义自己的View
- IOS把Single View App改成Navigation View app
- 创建自己的View类-移动的Picture
- ios之xib创建view
- 善用iOS App中Web View控件的好处
- 善用iOS App中Web View控件的好处
- 自定View-仿IOS安装app时的效果
- iphone实现图片部分拉伸(与android9图类似)
- org.apache.solr.common.SolrException: ERROR: multiple values encountered for non multiValued copy
- [CloudBox] 环状卷动背景控件
- 软核,硬核、固核的区别!
- 收集oracle统计信息
- ios app: view: 怎样创建自己的view
- foj1081等分液体
- Java 解析 XML
- c++多线程编程安全性简单说明
- telnet backdoor
- Ubuntu 系统下终端快捷键设置
- 由在EBS中收集分区表统计信息引发的思考
- 职业规划案例
- SQL Server 2012中快速插入批量数据的示例及疑惑