UIButton *button1 = [[UIButton alloc] init];
//设置字体大小
button1.titleLabel.font = [UIFont systemFontOfSize:20];
//设置selected属性强制让按钮处于选中状态
button1.selected = YES;
//设置enabled属性强制按钮可用与否button1.enabled = NO;
//参数red green blue的取值范围为0-1+ (UIColor *)colorWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
//为按钮添加点击事件- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
//随机颜色UIColor *color = [UIColor colorWithRed:(arc4random()%256)/255.0 green:(arc4random()%256)/255.0 blue:(arc4random()%256)/255.0 alpha:1];
UILabel
//设置文本对齐方式
textAlignment属性
//UIImage与UIImageView,UIImage是用来加载图片,UIImageView更像是一个容器用来显示图片
UIImage *bg = [UIImage imageNamed:@"map.png”];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)];
bgView.image = bg;
[self.view addSubview:bgView];
//将多张图片连续显示以达到动画的效果
UIImageView *penguin = [[UIImageView alloc] initWithFrame:CGRectMake(160,200,42,46)];
//保证动画一开始就有一张图片显示//imagesArr数组存放的是所有的图片
penguin.image = [imagesArr firstObject];
//给企鹅视图添加一组动画图片
//设置按钮tag,在点击事件中获得penguin这个imageView
penguin.animationImages = imagesArr;
penguin.tag = 1000
//设置动画执行一次的时间
penguin.animationDuraion = 1;
[self.view addSubView:penguin];
//设置按钮点击动画运行- (void)onClick:(UIButton *)bt
{
//根据tag和父视图获取子视图 viewWithTag:
UIImageView *penguin = (UIImageView *)[self.view viewWithTag:1000];
//判断视图动画有没有在运行
if (penguin.isAnimating) {
//运行 我就终止动画
[penguin stopAnimating];
penguin.frame = CGRectMake(160, 200, 42, 46);
//销毁当前定时器
[animationTimer invalidate];
//设置成开始 当停止时
[bt setTitle:@"开始" forState:UIControlStateNormal];
}else{
//没在运行 开始动画
[penguin startAnimating];
/**
* 使用类方法得到一个定时器的对象
*
* @param go
参数1 回调方法调用的频率
参数2 回调函数对应的类的对象
参数3 定期回调的函数
参数4 参数3 是否要携带参数
参数5 是否循环执行
*
* @return NSTimer
*/
animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(go) userInfo:nil repeats:YES];
//按钮设置 停止
[bt setTitle:@"停止" forState:UIControlStateNormal];
}
}
//让penguin走起
- (void)go
{
UIImageView *penguin = (UIImageView *)[self.window viewWithTag:1000];
//改坐标
penguin.frame = CGRectMake(penguin.frame.origin.x - 2, penguin.frame.origin.y, penguin.frame.size.width, penguin.frame.size.height);
}