主程序
#import "MainViewController.h"
@interface MainViewController ()
//不需要外界知道的属性,不要在.h里声明,在此处
@property(nonatomic,retain) UIImageView *imageview;
@end
@implementation MainViewController
- (void)dealloc
{
[super dealloc];
}
- (instancetype)init
{
[self initWithNibName:nil bundle:nil];
return self;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)loadView
{
[super loadView];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createControl];
// [self pageControl];
}
//作业2:关灯游戏第三种方法:用 tapgesture 来做
- (void)pageControl
{
// UIScrollview *scroll =
// UIPageControl
UIPageControl *page = [[UIPageControl alloc] initWithFrame:CGRectMake(20,20, 100, 200)];
[page setBackgroundColor:[UIColor redColor]];
page.hidesForSinglePage = NO;
[self.view addSubview:page];
[page release];
}
//创建控件
- (void)createControl
{
//滑动栏
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(20, 20, 300, 20)];
[self.view addSubview:slider];
[slider release];
// UIImageView;
// UISegmentedControl;//六个词: 点击,长按,清扫,捏合,旋转,拖拽
//给属性赋值一定要用self.给引用计数加1
self.imageview = [[UIImageView alloc] initWithFrame:CGRectMake(30, 50, 250, 350)];
self.imageview.userInteractionEnabled = YES;//用户交互改成yes,允许
[_imageview setImage:[UIImage imageNamed:@"miao.jpg"]];
[self.view addSubview:_imageview];
[_imageview release];
//
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"点击",@"长按",@"清扫",@"捏合",@"旋转",@"拖拽", nil];
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:array];
seg.frame = CGRectMake(0, 480, self.view.frame.size.width, 40);
[seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
//segment格子
[self.view addSubview:seg];
[seg release];
//for循环创建图片imageview时 添加手势
}
#pragma mark -
#pragma mark seg 点击事件
- (void)segAction:(id)tap
{
//分段控件 点击段
//移除imageview的所有手势,将tap手势给seg
UISegmentedControl *seg = (UISegmentedControl *)tap;
//1.获得一个视图的所有手势
NSArray *gestures = self.imageview.gestureRecognizers;
//2.移除
for (UIGestureRecognizer *tap in gestures) {
[self.imageview removeGestureRecognizer:tap];
}
//点击: 给图片添加一个点击手势,换掉图片
switch (seg.selectedSegmentIndex) {
case 0:
{
//3.需要先创建一个手势
//点击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[self.imageview addGestureRecognizer:tap];
[tap release];
}
break;
case 1:
{
//长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[self.imageview addGestureRecognizer:longPress];
[longPress release];
}
break;
case 2:
{
//清扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//一个清扫手势只能设置一个方向
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.imageview addGestureRecognizer:swipe];
[swipe release];
}
break;
case 3:
{
//捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[self.imageview addGestureRecognizer:pinch];
[pinch release];
}
break;
case 4:
{
//旋转
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];
[self.imageview addGestureRecognizer:rotate];
[rotate release];
}
break;
case 5:
{
//拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[self.imageview addGestureRecognizer:pan];
[pan release];
}
break;
default:
break;
}
}
#pragma mark -
#pragma mark 拖拽手势事件
- (void)panAction:(UIPanGestureRecognizer *)pan
{
//获得当前偏移量(拖拽的距离)
CGPoint point = [pan translationInView:self.imageview];
//还是相对于当前的位置进行偏移,越加越大,所以偏移完要把偏移量的值还原到0
self.imageview.transform = CGAffineTransformScale(self.imageview.transform, point.x, point.y);
[pan setTranslation:CGPointZero inView:self.imageview];//CGPointZero 是(0,0)
}
#pragma mark -
#pragma mark 捏合手势事件
- (void)pinchAction:(UIPinchGestureRecognizer *)ges
{
self.imageview.transform = CGAffineTransformScale(self.imageview.transform, ges.scale, ges.scale);//根据self.imageview.transform scale是捏合的比例
NSLog(@"scale == %f",ges.scale);
//是以当前的状态为基准来缩小的,所以每次都要归到原始比例1
ges.scale = 1.0f;
}
#pragma mark -
#pragma mark 清扫手势事件
- (void)swipeAction:(UISwipeGestureRecognizer *)ges
{
if (ges.state == UIGestureRecognizerStateBegan) {
NSLog(@"swipe begin");
}
else if(ges.state == UIGestureRecognizerStateEnded)
{
NSLog(@"swipe end");
}
}
#pragma mark -
#pragma mark 长按手势事件
- (void)longPressAction:(UILongPressGestureRecognizer *)ges
{
if (ges.state == UIGestureRecognizerStateBegan) {
NSLog(@"aaa");
}
else if(ges.state == UIGestureRecognizerStateEnded)
{
NSLog(@"bbb");
}
}
#pragma mark -
#pragma mark 点击手势事件
- (void)tapAction:(UITapGestureRecognizer *)tap
{
//把手势添加到imageview上
//通过手势获得所在的视图
// UIImageView *aImage = (UIImageView *)tap.view;
if (tap.state == UIGestureRecognizerStateBegan) {//手势点击的状态(枚举)
NSLog(@"开始点击");
//更换图片等
}
[_imageview setImage:[UIImage imageNamed:@"mi.jpg"]];
//再点击图片又调回上一张图
NSLog(@"%s",__func__);
}
#pragma mark -
#pragma mark 旋转手势事件
- (void)rotateAction:(UIRotationGestureRecognizer *)ges
{
//transform 每个变量是什么意思 到底该怎么用
//CGAffineTransform 是函数
self.imageview.transform = CGAffineTransformRotate(self.imageview.transform, ges.rotation);
//ges.rotation = 0;//(相对于图片自身角度一直是0)
NSLog(@"rotation == %f",ges.rotation);//ges.rotation 旋转的角度:是基于最初的方向(沿着图片竖直向上)旋转了多少度
//下次旋转以目前的状态为基础,基准点不一样
// UIImageView *bImage = (UIImageView *)rotate.view;
// if (rotate.state == UIGestureRecognizerStateBegan) {//手势点击的状态(枚举)
// NSLog(@"开始旋转");
//
// //更换图片等
// }
}