UIView设计小技巧

键盘收起

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

快速删除所有子控件

[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

UIView设置 部分圆角

-(void)setTopLeftTopRightCornerRadius:(float)radius{
    
    //设置所需的圆角位置以及大小
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds 
                              byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight 
                              cornerRadii:CGSizeMake(radius, radius)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;  
}

采用图片作backgroundColor

// 有特殊效果的图片
_View.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];

view添加虚线框

    CAShapeLayer *border = [CAShapeLayer layer];
    border.strokeColor = [UIColor colorWithRed:67/255.0f green:37/255.0f blue:83/255.0f alpha:1].CGColor;
    border.fillColor = nil;
    border.lineDashPattern = @[@4, @2];
    border.path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
    border.frame = view.bounds;
    [view.layer addSublayer:border];

圆角
    CAShapeLayer *border = [CAShapeLayer layer];
    
    border.strokeColor = [UIColor redColor].CGColor;  //虚线的颜色
    border.fillColor = nil;  //填充的颜色
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.lineButton.bounds cornerRadius:5];
    
    border.path = path.CGPath;   //设置路径
    
    border.frame = self.lineButton.bounds;
 
    border.lineWidth = 1.f;   //虚线的宽度
    
    //设置线条的样式
    //    border.lineCap = @"square";
    
    border.lineDashPattern = @[@4, @2];    //虚线的间隔
    
    self.lineButton.layer.cornerRadius = 5.f;
    self.lineButton.layer.masksToBounds = YES;
    
    [self.lineButton.layer addSublayer:border];

移除边框
    [border removeFromSuperlayer];

判断View是否某个View的子视图

BOOL isView = [textView isDescendantOfView:self.view];

子控件超出时无法点击

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {  
    UIView *view = [super hitTest:point withEvent:event];  
     
    if (view == nil) {  
          //将坐标由当前视图发送到 指定视图 fromView是无法响应的范围小父视图
         CGPoint stationPoint = [_stationTypeChooseView.tableView convertPoint:point fromView:self];  
       
        if (CGRectContainsPoint(_stationTypeChooseView.tableView.bounds, stationPoint))  
  
                     view = _stationTypeChooseView.tableView;    
          
    }  
    return view;  
}  

点击响应是从从底部往上依次传递的,当遇到其中一个无法传递,那么便跳过它,传递给下一个能传递的或者能响应的。

设置label的旋转角度

label.transform = CGAffineTransformMakeRotation(0.1);    

常见控件的监听:
1.只要控件是继承UIControl就有addTarget监听方法,如,按钮监听点击事件:

[btn  addTarget:self  action:@selector(clickbtn:)  forControlEvents:UIControlEventTouchUpInside];     

其中clickbtn:为点击事件发生后所要执行的方法(可自定义)。

位置frame、bounds、center概念区分

frame:以父控件左上角为坐标原点,在其父控件中的位置和尺寸。
bounds:以自己左上角为坐标原点(x=0,y=0),控件的位置和尺寸。
center:以父控件的左上角为坐标原点,其控件中点的位置。

其他

http://blog.csdn.net/deft_mkjing/article/details/52048077

图片轮播器的使用

http://blog.csdn.net/lurenjia_kb/article/details/51567072
http://blog.csdn.net/zhengyanfeng1989/article/details/51508280

扩大按钮的点击范围

http://www.jianshu.com/p/43c22fa3b42c

你可能感兴趣的:(UIView设计小技巧)