//添加初始图片
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 250, 300)];
imgView.center = CGPointMake(160, 240);
imgView.image = [UIImage imageNamed:@"1.jpg"];
imgView.tag = 101;
[self.window addSubview:imgView];
[imgView release];
/*********************************/
//默认UIImageView无法接收手势
imgView.userInteractionEnabled = YES;
/*********************************/
/*
手势:点击 拖拽 缩放 长按 方向 旋转
*/
//点击手势
UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTap:)];
// 默认是单击手势
// 双击手势
tap.numberOfTapsRequired = 2;
//添加手势
[imgView addGestureRecognizer:tap];
//添加标题
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(35, 60, 250, 30)
];
label.textAlignment = NSTextAlignmentJustified;
label.tag = 100;
[self.window addSubview:label];
label.text = @"1.jpg";
NSArray *arr = @[@"上一张",@"下一张"];
[label release];
for (int i =0; i<2; i++) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
b.frame = CGRectMake(35 +150*i, 400, 100, 30);
[b setTitle: arr[i] forState:UIControlStateNormal];
[b setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[b addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
b.tag = 100 + i + 2;
[self.window addSubview:b];
}
return YES;
}
/*
tag:
100:标题
101:图片
102:上一张
103:下一张
*/
int curPage = 1;
-(void)click:(UIButton *)b
{
//通过tag来获取window上得UILabel,UIView
UILabel *label = (UILabel *)[self.window viewWithTag:100];
UIImageView *imgView = (UIImageView *)[self.window viewWithTag:101];
if (b.tag == 102) {
curPage--;
if (curPage<1) {
curPage = 8;
}
}else{
curPage++;
if (curPage>8) {
curPage = 1;
}
}
// 获取图片名的字符串
NSString *str =[NSString stringWithFormat:@"%d.jpg",curPage];
imgView.image = [UIImage imageNamed:str];
label.text = str;
// str图片名 根据图片名获取图片
imgView.image = [UIImage imageNamed:str];
}
BOOL tap = NO;
- (void)myTap:(UITapGestureRecognizer *)obj
{
// 手势可以用作的UIView传递下来
NSLog(@"%@",obj.view);
// 可以将当前手势作用的位置传递下来
// 参数是哪个视图 就会转化为该视图的坐标
CGPoint point = [obj locationInView:obj.view];
//点击一下就放大
UIImageView * imgView = (UIImageView *)obj.view;
//添加动画
//开始动画
[UIView beginAnimations:nil context:nil];
//设置动画持续时间
[UIView setAnimationDuration:1.0];
if (tap) {
imgView.frame = CGRectMake(0, 0, 320, 480);
//之余顶层
[self.window bringSubviewToFront:imgView];
}else{
imgView.frame = CGRectMake(0, 20, 250, 300);
imgView.center = CGPointMake(160, 240);
}
tap = !tap ;
//提交动画
[UIView commitAnimations];
}