UIView 详解

- (CGSize)sizeThatFits:(CGSize)size;     // return 'best' size to fit given size. does not actually resize view. Default is return existing view size

- (void)sizeToFit;                       // calls sizeThatFits: with current view bounds and changes bounds size.

 

sizeThatFits:

 

Asks the view to calculate and return the size that best fits its subviews.

- (CGSize)sizeThatFits:(CGSize)size
Parameters
size

The current size of the receiver.

Return Value

A new size that fits the receiver’s subviews.

Discussion

The default implementation of this method returns the size portion of the view’s bounds rectangle. Subclasses can override this method to return a custom value based on the desired layout of any subviews. For example, a UISwitch object returns a fixed size value that represents the standard size of a switch view, and a UIImageView object returns the size of the image it is currently displaying.

This method does not resize the receiver.

Availability
  • Available in iOS 2.0 and later.
 
 

sizeToFit

Resizes and moves the receiver view so it just encloses its subviews.

- (void)sizeToFit
Discussion

Call this method when you want to resize the current view so that it uses the most appropriate amount of space. Specific UIKit views resize themselves according to their own internal needs. In some cases, if a view does not have a superview, it may size itself to the screen bounds. Thus, if you want a given view to size itself to its parent view, you should add it to the parent view before calling this method.

You should not override this method. If you want to change the default sizing information for your view, override the sizeThatFits: instead. That method performs any needed calculations and returns them to this method, which then makes the change.

Availability
  • Available in iOS 2.0 and later.
个人应用:
- (void)sizeToFit得到最适合当前字数的尺寸,
1   新建一个UILabel *label,尺寸CGRectMake(10,10,150,20);    [label setNumberOfLine:1];    [label setBackgroundColor:[UIColor clearColor];
2   之后从键盘输入到UITextField中一定数量文字,将文字赋值给label;
3   对label使用 方法   - (void)sizeToFit;   当前label的尺寸改变,行数也改变了,根据最新的尺寸可添加相应尺寸的背景图片。
 
在UIView中添加点击事件 (通过添加手势识别来实现)
首先设定UIView(或其子类)为可交互的:

view.userInteractionEnabled = YES;  
添加tap手势:
 
//tap手势  
       UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(event:)];  
 
将手势添加至需要相应的view中
 
view addGestureRecognizer:tapGesture];  
 
默认为单击触发事件:
设置手指个数:
  
[tapGesture setNumberOfTapsRequired:2];  
 
执行触发的方法:

- (void)event:(UITapGestureRecognizer *)gesture  
{  

NSLog(@"单机");  
 }

获取是哪个View触发了此方法:

gesture.view ;  
 
OK ,基本的手势使用方法就这些了,希望对你有所帮助

参考:http://www.2cto.com/kf/201301/186169.html

http://fengmm521.blog.163.com/blog/static/2509135820134157252907/

 

为uiview添加动画效果(非页面切换效果哦),可以单独为某个view设置其出现或消失的效果。注意,设置动画的时机非常重要。尽量将设置在view的数据已经准备充分,马上就要加载之前,不要让设置动画与数据加载之间相隔太久,否则动画效果就没效果了。因为view执行动画效果是有一个时间过程的。如果在这个时间中,页面数据没有加载上,那么动画就已经执行完了。这样不就达不到我们预期的效果了吗。建议:将动画设置在页面数据已经准备完成,马上要显示之前。

    //此处设置self.view动画时机最佳

    [self.view.layer addAnimation:[SwitchView useAnimation:101 from:@"right"] forKey:@"animation"];

/****

马上加载页面上的数据

******/

 

 
 
posted @ 2013-02-28 10:45  ygm900  阅读(2234)  评论(0编辑  收藏  举报