于AppDelegate.m档,创建一个视图控制器

#import "MAYAppDelegate.h"

#import "MAYViewController.h"

@implementation MAYAppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    

    MAYViewController *root = [[MAYViewController alloc] init];

    self.window.rootViewController = root;

    [root release];

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;

}



创建一个视图控制器MAYViewController,在视图控制器MAYViewController.m文件里,创建一个视图

#import "MAYViewController.h"

#import "Touch.h"

@interface MAYViewController ()



@end



@implementation MAYViewController



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

      

    }

    return self;

}



- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor lightGrayColor];

    Touch *touch = [[Touch alloc] initWithFrame:CGRectMake(50, 150, 200, 200)];

    touch.backgroundColor = [UIColor greenColor];

    [self.view addSubview:touch];

    [touch release];

    

}



新建一个视图类Touch,加入在视图控制器上,在Touch.m文件里实现缩放功能

#import "Touch.h"



@implementation Touch



- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        //设置是否支持多点触摸(YES支持,NO不支持)

        self.multipleTouchEnabled = YES;

    }

    return self;

}

//计算两点之间的距离

- (CGFloat)distance:(CGPoint)point1 point2:(CGPoint)point2

{

    CGFloat dx = point1.x - point2.x;

    CGFloat dy = point1.y - point2.y;

    CGFloat tance = sqrt(pow(dx, 2) + pow(dy, 2));

    return tance;

}





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

{

    

    if (1 == [touches count]) {

        return;

    }

    //设置触摸点

    NSArray *array = [touches allObjects];

    UITouch *touch1 = [array firstObject];

    UITouch *touch2 = [array lastObject];

    //获取移动前的触摸点

    CGPoint firstPreviousPoint = [touch1 previousLocationInView:self];

    CGPoint secondPreviousPoint = [touch2 previousLocationInView:self];

    

    //获取与移动后的触摸点

    CGPoint firstCurrentPoint = [touch1 locationInView:self];

    CGPoint secondCurrentPoint = [touch2 locationInView:self];

    

    //获取移动前后的两点之间的距离

    CGFloat previousDistance = [self distance:firstPreviousPoint point2:secondPreviousPoint];

    CGFloat currentDistance = [self distance:firstCurrentPoint point2:secondCurrentPoint];

    

    //获取缩放比例

    CGFloat scanl = currentDistance / previousDistance;

    

    //获得缩放后的视图大小

    

    self.bounds = CGRectMake(0, 0, self.bounds.size.width * scanl, self.bounds.size.height * scanl);

    }

版权声明:本文博主原创文章,博客,未经同意不得转载。

最新文章

  1. Win 10 文件浏览器无法打开
  2. moffiestyle
  3. PPC MPC85xx e500学习笔记
  4. git中通过实际操作来了解常用命令
  5. Highcharts导出gb2312乱码问题
  6. codevs3872 邮递员送信(SPFA)
  7. BitMask 使用参考
  8. Delphi richedit获取选中文字
  9. struts validate
  10. 【git】切换分支获取代码
  11. javascript GB2312转UTF8
  12. JDBC连接Oracle数据库时出现的ORA-12505错误及解决办法
  13. SQLite基础回顾
  14. org.springframework.web.servlet.PageNotFound
  15. linux java 和jmeter 环境变量配置文件笔记(原)
  16. 收藏一个带动画效果的ScrollViewer以及ScrollBar的模板
  17. MFC头文件
  18. 第14月第30天 svn 撤销ignore revert
  19. 连接oracle数据库报错:TNS-12516 TNS:listener could not find available handler with matching protocol stack解决方法
  20. 实战分析: MySQL字符集

热门文章

  1. 苹果被拒的血泪史。。。(update 2015.11)
  2. nodejs原生模块简介
  3. cocos2d-x 实现沙子特效
  4. hdu 3371 Connect the Cities (最小生成树Prim)
  5. 【COGS 56】质数取石子
  6. js Math函数
  7. html中插入flash代码详解(转载)
  8. 在PyQt4中使用matplotlib
  9. net core 依赖注入问题
  10. mongodb3.0 db.addUser报错