ios app: view: 怎样创建自己的view

我是个不喜欢普通的人,Cocao 的common view 设计的很好。也许是审美疲劳,从开始准备写App时我就准备自己写view,让一切看起来与众不同。我也不想分享什么,也不是想帮助任何人。我只是纪录下我学的帮助自己理解,理清自己的思路。


什么解释都不如代码来的快:

  1. 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;
    }
  2. dealloc 是需要的,release 你创建的东西,每人想要memory leak。
    - (void)dealloc {
    	[placardImage release];
    	[currentDisplayString release];
    	[displayStrings release];
    	[super dealloc];
    }
  3. 最重要的是-(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]”.当绘图周期到的时候,图像就被改变了,很快,快的几乎不让你感觉到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值