iOS 手势

-(void)viewDidLoad

{

    [super viewDidLoad];

    imageView=[[UIImageView alloc] initWithFrame:CGRectMake(100, 150, 120, 120)];

    imageView.image=[UIImage imageNamed:@"2.jpg"];

    [self.view addSubview:imageView];

    [imageView release];

    imageView.userInteractionEnabled=YES;

    //-----添加手势——点击

    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];//tap :点击|gesture:手势|Recognizer:承认者/响应者

    [imageView addGestureRecognizer:tap];

    tap.numberOfTapsRequired=2;//需要点击几次? required:需求

    tap.numberOfTouchesRequired=2;//几个触点啊? touches:触点

    [tap release];

    //-----添加手势——平移

    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];//pan:

    [imageView addGestureRecognizer:pan];

    [pan release];

    //------添加手势——旋转

    UIRotationGestureRecognizer * rotation=[[UIRotationGestureRecognizer alloc] initWithTarget:selfaction:@selector(rotation:)];//Rotation:旋转

    [imageView addGestureRecognizer:rotation];

    //------添加手势——收缩

    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];

    [imageView addGestureRecognizer:pinch];

    [pinch release];

    [rotation release];

}

 //收缩实现——//bounds:界限 |scale :比例 友情提示,bounds 中心点不改变X,Y轴的距离,只改变长宽。

-(void)pinch:(UIPinchGestureRecognizer *)pinch

{

    imageView.bounds=CGRectMake(0, 0, imageView.bounds.size.width*pinch.scale,imageView.bounds.size.height*pinch.scale);//bounds:界限 |scale :比例 友情提示,bounds 中心点不改变X,Y轴的距离,只改变长宽。

    pinch.scale=1.0;

}

-(void)pan:(UIPanGestureRecognizer *)pan

{

    CGPoint point=[pan translationInView:self.view];// translation :平移 获取相对坐标原点的坐标

    NSLog(@"%f,%f",point.x,point.y);

    imageView.center=CGPointMake(imageView.center.x+point.ximageView.center.y+point.y);

    [pan setTranslation:CGPointZero inView:self.view];//设置坐标原点位上次的坐标

}

   //旋转

-(void)rotation:(UIRotationGestureRecognizer *)rotation

{

    imageView.transform=CGAffineTransformMakeRotation(finRotation+rotation.rotation);//transform :弧度改变 |affine:仿射  弧度改变

    if(rotation.state==UIGestureRecognizerStateEnded)//监听响应者的状态,判断是否结束

    {

        finRotation+=rotation.rotation;//弧度修改

    }

    NSLog(@"元方你怎么看?%f",rotation.rotation);

}

-(void)tap:(UITapGestureRecognizer *)tap

{

    NSLog(@"tap");

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。