现在的位置: 首页 > 综合 > 正文

触摸与手势

2018年02月14日 ⁄ 综合 ⁄ 共 4560字 ⁄ 字号 评论关闭

     在这个智能机的时代,触摸与手势应该是最基本的了。对于简单的点击控件而触发各种事件的触摸这里就不说了(比如点个按钮,点个文本框,表格的滑动,直接设置某些属性,链接某个函数就OK了)。这里主要写下关于图片的缩放。可以点击放大,缩小,根据手势扩大和缩小。就类似于QQ里的那种查看图片。

首先,我们得了解,触摸的操作对象是View而不是Controller。想想,如果是整个controller,在整个界面都会响应,那就麻烦了。

当我们在屏幕上触摸,比划手势,UIResponder类会响应这些触摸。我们通过重写它的方法而达到我们想要的效果


所以,我们首先得创建一个View的类,继承自UIView。

@interface DragView :UIImageView

{

   CGPoint ptStartLocation;

   BOOL bLarge;

   float wRate;

   float hRate;

   float originWidth;

   float originHeight;

   CGFloat lastScale;

   

}

@property (assign,nonatomic)
float originWidth;

@property (assign,nonatomic)
float originHeight;

@property (assign,nonatomic)
float rate;

//下面两个方法是重写

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;

@end

准备好一张大图

在ViewDidload里面加载图片,并缩小图片

    drag = [[DragViewalloc]
initWithImage:[UIImageimageNamed:@"conan.jpg"]];

    drag.originHeight =drag.frame.size.height;

    drag.originWidth =drag.frame.size.width;

    float showRate; //这个用来限定初始显示的图片的大小

    if (drag.originHeight -44
>
drag.originWidth)

    {

        showRate = SHOWSIZE /
drag
.originHeight;

    }

    else

    {

        showRate = SHOWSIZE /
drag
.originWidth;

    }

    //记录下水平与垂直方向的比例

    float hRate =
self
.view.frame.size.height /drag.originHeight

    float wRate =
self
.view.frame.size.width /drag.originWidth;

    if(wRate > hRate)

    {

        drag.rate = wRate;

    }

    else

    {

        drag.rate = hRate;

    }

    

    //[self.view addSubview:drag];

    _scrollView.contentSize =CGSizeMake(drag.originWidth,drag.originHeight);

    _scrollView.contentOffset =CGPointMake(drag.originWidth/2,drag.originHeight/2);

    drag.frame =CGRectMake(20+_scrollView.contentOffset.x,100+_scrollView.contentOffset.y,drag.originWidth*showRate,drag.originHeight*showRate);

    [_scrollView
addSubview:drag];

    UIPinchGestureRecognizer *gesture = [[UIPinchGestureRecognizeralloc]
initWithTarget:selfaction:@selector(scale:)];

    [dragaddGestureRecognizer:gesture];

   

    gesture.delegate =
self
;

    [gesture release];

    [drag release];

手势的代理函数

-(BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch
*)touch

{

    return
YES;

}

下面就是手势缩放的处理

-(void) scale:(id) sender //缩放

{

    //根据角度缩放

    if ([(UIPinchGestureRecognizer *) senderstate] ==
UIGestureRecognizerStateBegan)

    {

       lastScale =
1.0;

    }

   else

    {

       CGFloat scale =
lastScale - ((1.0 -[(UIPinchGestureRecognizer *) senderscale])/2) ;

       NSLog(@"scale:%f",scale);

       CGAffineTransform currentTransform =
drag.transform;

       CGAffineTransform newTransfrom =
CGAffineTransformScale(currentTransform, scale, scale);

        [dragsetTransform: newTransfrom];

       lastScale = [(UIPinchGestureRecognizer *) senderscale];

         NSLog(@"lastscale:%f",lastScale);

    

    }

}

特别说明:CGAffineTransform

大家或许对CGAffineTransform不是很了解。我也不是很了解。查了些资料,在纸上画呀,算呀的研究了下,明白了一点点。

它其实是一个矩阵,仿射变换矩阵

起始点:orgin[X,Y,1] transform |a b 0|

       |c d 0|

         |tx ty 1|

orgin = [aX + cY+tx,bX+dY+ty];


下面是图片的3种基本的操作

缩放

CGAffineTransformScale(currentTransform, scale, scale);


scale,scale是X,Y上面的缩放量。如果要使图片不变形,X,Y上的缩放量应该是一致的。

旋转

CGAffineTransformRotate(transform, scale);

transform表示以此transform为基础

scale代表旋转角度


平移

 CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty);

tx:X方向上的平移量

ty:Y方向上的平移量

再来看下我们新建的类重写的UITouch的几个函数。这里只重写touchesEnded函数,因为上面加载图片时使用了UISCrollerView.

图片加载在ScrollerView里面。ScrollerView已经重写了那些函数,我们就不必再写了。


-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event //触摸接触

{

   UITouch *touch = [touches
anyObject]; //这里从一个集合中取一个touch

    //单击事件处理,使图片与屏幕一样大

   if (touch.tapCount ==1)

    {

        self.frame =CGRectMake(0.0f,0.0f
,
originWidth * rate,originHeight *
rate);

       self.center =CGPointMake([selfsuperview].frame.size.width/2
+originWidth/2, [selfsuperview].frame.size.height
/2 +originHeight/2);

        [UIApplicationsharedApplication].statusBarHidden =NO;

    }

    //双击事件处理,扩大图片为原图的大小

   if(touch.tapCount ==2)

    {

       if (bLarge)

        {

           bLarge = !bLarge;

           self.frame =CGRectMake(0.0f,40.0f
,originWidth * rate,originHeight *
rate);

            [UIApplicationsharedApplication].statusBarHidden =NO;

           self.center =CGPointMake(originWidth/2,originHeight/2);

        }

       else

        {

           bLarge = !bLarge;

           self.frame =CGRectMake(0.0f,-20.0f
,originWidth, originHeight);

            [UIApplicationsharedApplication].statusBarHidden =YES;

           self.center =CGPointMake(originWidth/2,originHeight/2);

        }

        

    }

如果没有用到scrollerView,则重写begin和moved函数

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event //触摸开始

{

   CGPoint pt = [[touches
anyObject] locationInView:self];

    ptStartLocation = pt;

    [[selfsuperview]
bringSubviewToFront:self];

}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//触摸移动过程中

{

    // Calculate offset

CGPoint pt = [[touchesanyObject]
locationInView:self];

float dx = pt.x -ptStartLocation.x;

float dy = pt.y -ptStartLocation.y;

CGPoint newcenter =CGPointMake(self.center.x
+ dx,self.center.y + dy);

// Set new location

   self.center = newcenter;

}

抱歉!评论已关闭.