-(void)createButtons
{
for (int i = 0; i < 9; i++)
{
UIButton *button = [[UIButton alloc]init];
button.frame = CGRectMake(60 + (i % 3) * 100, 100 + (i / 3) * 100, 100, 100);
NSString *imageName = [NSString stringWithFormat:@"2_%d.jpg",i + 1];
//设置背景图片
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
//设置高亮状态的背景图片
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
//添加点击事件
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
}
//把点击的button放到最表层
[self.view bringSubviewToFront:button];
//用来记住放大之前的位置和大小
static CGRect frame;
//如果是没有放大色状态
if (button.frame.size.width != self.view.frame.size.width) {
//记录放大之前的位置和大小
frame = button.frame;
//开始动画块的标志,放大到全屏
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
button.frame = self.view.bounds;
[UIView commitAnimations];
}else
{
//如果已经放大,缩小并放回原来的位置
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
//把记录的原位置赋回去
button.frame = frame;
[UIView commitAnimations];
}
网友评论