UIVIew实战
前一篇博客介绍了UIView原理知识,在本篇博客中将重点介绍UIVIew应用方面的知识。
1.创建一个view
CGRect viewRect = CGRectMake(10,10,100,100);
UIView *myView = [[UIView alloc] initWithFrame:viewRect];
DEMO:
#import "ZSViewController.h"
#define BVWIDTH 300
#define BVHEIGHT 300
#define MVWIDTH 200
#define MVHEIGHT 200
#define TVWIDTH 100
#define TVHEIGHT 100
@interface ZSViewController ()
@end
@implementation ZSViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *buttomView = [self createViewWithRect:CGRectMake(self.view.center.x-BVWIDTH/2, self.view.center.y-BVHEIGHT/2, BVWIDTH, BVHEIGHT)];
[buttomView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:buttomView];
UIView *topView = [self createViewWithRect:CGRectMake(self.view.center.x-TVWIDTH/2, self.view.center.y-TVHEIGHT/2, TVWIDTH, TVHEIGHT)];
[topView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:topView];
UIView *midView = [self createViewWithRect:CGRectMake(self.view.center.x-MVWIDTH/2, self.view.center.y-MVHEIGHT/2, MVWIDTH, MVHEIGHT)];
[midView setBackgroundColor:[UIColor yellowColor]];
[self.view insertSubview:midView aboveSubview:buttomView];
}
/**
* create a new view with rect
*/
- (UIView *)createViewWithRect:(CGRect)rect{
UIView *view = [[UIView alloc] initWithFrame:rect];
return view;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果图:
drawRect Demo:
#import "ZSCircleView.h"
@implementation ZSCircleView
- (void)drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];
[self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];
[self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];
[self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];
[self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];
[self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];
[self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];
[self drawCircleWithPoint:CGPointMake(arc4random()%300, arc4random()%500) context:context];
}
- (void)drawCircleWithPoint:(CGPoint)pos context:(CGContextRef)context{
UIGraphicsPushContext(context);
CGContextSetLineWidth(context, 10);
[[UIColor colorWithRed:arc4random()%255/100 green:arc4random()%255/100 blue:arc4random()%255/100 alpha:1] setStroke];
CGContextBeginPath(context);
CGContextAddArc(context, pos.x, pos.y, arc4random()%100+10, 0, 2*M_PI, YES);
CGContextStrokePath(context);
UIGraphicsPopContext();
}
@end
效果图:
objective-c 中三种产生随机数的方法
//arc4random() 比较精确不需要生成随即种子
//通过arc4random() 获取0到x-1之间的整数的代码如下:
int value = arc4random() % x;
//获取1到x之间的整数的代码如下:
int value = (arc4random() % x) + 1;
//CCRANDOM_0_1() cocos2d中使用 ,范围是[0,1]
float random = CCRANDOM_0_1() * 5; //[0,5] CCRANDOM_0_1() 取值范围是[0,1]
//random() 需要初始化时设置种子
srandom((unsigned int)time(time_t *)NULL); //初始化时,设置下种子就好了。
注意:要做一个手绘板的话不能直接在drawRect里操作,应当使用类UIBezierPath
综合DEMO:
#import "RichRandomSmallBall.h"
#define RANDOMBALLX 150
#define RANDOMBALLY 100
@interface RichRandomSmallBall(){
CGAffineTransform first ;
CGAffineTransform second ;
CGAffineTransform third ;
}
@property (nonatomic,strong)UIImage *img;
@end
@implementation RichRandomSmallBall
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
self.backgroundColor = [UIColor greenColor];
self.clipsToBounds = YES;
[self setup];
}
- (void)setup{
self.img = [UIImage imageNamed:@"circle.png"];
self.first_ball = [[UIImageView alloc] initWithImage:self.img];
self.second_ball = [[UIImageView alloc] initWithImage:self.img];
self.third_ball = [[UIImageView alloc] initWithImage:self.img];
[self addSubview:self.first_ball];
[self addSubview:self.second_ball];
[self addSubview:self.third_ball];
[self initRandomBall];
[self setRandomBallPosition];
[self randomBallAnimate];
}
- (void)initRandomBall{
self.first_ball.frame = CGRectMake(self.center.x-RANDOMBALLX/2, -RANDOMBALLY/2, RANDOMBALLX , RANDOMBALLY);
self.second_ball.frame = CGRectMake(-RANDOMBALLX/2, self.frame.size.height-RANDOMBALLY/2, RANDOMBALLX, RANDOMBALLY);
self.third_ball.frame = CGRectMake(self.frame.size.width-RANDOMBALLX/2, self.frame.size.height-RANDOMBALLY/2, RANDOMBALLX, RANDOMBALLY);
self.first_ball.alpha = 0.3;
self.second_ball.alpha = 0.3;
self.third_ball.alpha = 0.3;
}
- (void) setRandomBallPosition{
first = self.first_ball.transform;
second = self.second_ball.transform;
third = self.third_ball.transform;
}
- (void)getRandomBallPosition{
self.first_ball.transform = first;
self.second_ball.transform = second;
self.third_ball.transform = third;
self.first_ball.alpha = 0.3;
self.second_ball.alpha = 0.3;
self.third_ball.alpha = 0.3;
}
- (void) randomBallAnimate{
[self getRandomBallPosition];
[UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
float x = self.center.x ;
float y = self.center.y ;
self.first_ball.transform = CGAffineTransformScale(CGAffineTransformTranslate(self.first_ball.transform,0 ,y), 1.5, 1.5);
self.second_ball.transform = CGAffineTransformScale(CGAffineTransformTranslate(self.second_ball.transform,x+RANDOMBALLX/2,-y), 1.5, 1.5);
self.third_ball.transform = CGAffineTransformScale(CGAffineTransformTranslate(self.third_ball.transform,-x-RANDOMBALLX/2 ,-y), 1.5, 1.5);
self.first_ball.alpha = 1;
self.second_ball.alpha = 1;
self.third_ball.alpha = 1;
} completion:^(BOOL finished){
}];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
#import "RichViewController.h"
#define RANDOMBALLX 150
#define RANDOMBALLY 100
@interface RichViewController ()
@end
@implementation RichViewController
- (IBAction)randomForMore:(UIButton *)sender {
[self.randomSmallBallView randomBallAnimate];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.randomSmallBallView.first_ball.userInteractionEnabled = YES;
self.randomSmallBallView.second_ball.userInteractionEnabled = YES;
self.randomSmallBallView.third_ball.userInteractionEnabled = YES;
[self.randomSmallBallView.first_ball addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(firstRandomBallTaped)]];
[self.randomSmallBallView.second_ball addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(secondRandomBallTaped)]];
[self.randomSmallBallView.third_ball addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(thirdRandomBallTaped)]];
UIImage *img = [UIImage imageNamed:@"bubble.png"];
// [img resizableImageWithCapInsets:UIEdgeInsetsMake(50, 50, 50, 50)];
// UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
// UIImageView *imgView = [[UIImageView alloc] initWithImage:[img stretchableImageWithLeftCapWidth:21 topCapHeight:14]];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[img resizableImageWithCapInsets:UIEdgeInsetsMake(15.5, 22, 15.5, 22)]];
imgView.frame = CGRectMake(0,400 , img.size.width+100, img.size.height+100);
UILabel *label = [[UILabel alloc] init];
label.text = @"hello world";
label.frame = CGRectMake(20, 0, 100, 100);
label.adjustsFontSizeToFitWidth = YES;
[imgView addSubview:label];
[self.view addSubview:imgView];
}
- (void)firstRandomBallTaped{
[self performSegueWithIdentifier:@"randomBallTaped" sender:self];
}
- (void)secondRandomBallTaped{
[self performSegueWithIdentifier:@"randomBallTaped" sender:self];
}
- (void)thirdRandomBallTaped{
[self performSegueWithIdentifier:@"randomBallTaped" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"randomBallTaped"]){
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end