通过两个触摸点实现视图的缩放(iOS)

在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);

}

时间: 2024-08-05 23:41:01

通过两个触摸点实现视图的缩放(iOS)的相关文章

UITouch/UIResponder:iOS上触摸事件的视图检测和事件传递

iPhone上有非常流畅的用户触摸交互体验,能检测各种手势:点击,滑动,放大缩小,旋转.大多数情况都是用UI*GestureRecognizer这样的手势对象来关联手势事件和手势处理函数.也有时候,会看到第三方代码里会在如下函数中进行处理: -(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event: 那么问题就来了,手势和touch到底有什么区别和联系?这一切还得从头iOS触摸事件检测,以及UIResponder(响应者)开始说起

两个数据库比较 对比视图存储过程及表结构差异

两个数据库比较 对比视图存储过程及表结构差异 一.视图和存储过程比较 [原理]利用系统表“sysobjects"和系统表“syscomments”,将数据库中的视图和存储过程进行对比.系统表"sysobjects"之前有详细介绍过,有兴趣可以看看:SQL Server系统表sysobjects介绍与使用 如果你看到这段文字,说明您正使用RSS阅读或转自<一棵树-博客园>,原文地址:http://www.cnblogs.com/atree/p/db-compare-

[翻译]Nativescript 中 Web 视图与 Android/IOS 的双向通信

English document From http://shripalsoni.com/blog/nativescript-webview-native-bi-directional-communication/ Nativescript 中 Web 视图与 Android/IOS 的双向通信 由shripal编写 在Nativescript中 Nativescript 提供跨平台的 web 视图 ui 元素.它为在我们的页面中显示 web 视图内容提供了服务.但是, 如果您希望将一些数据发送

(转载) 两个数据库比较 对比视图存储过程及表结构差异

一.视图和存储过程比较 [原理]利用系统表"sysobjects"和系统表"syscomments",将数据库中的视图和存储过程进行对比.系统表"sysobjects"之前有详细介绍过,有兴趣可以看看:SQL Server系统表sysobjects介绍与使用 [代码] /*--调用示例 exec p_compdb 'DBNAME1','DBNAME2' exec p_compdb 'DBNAME2','DBNAME3' --*/ CREATE p

要缩小通过两个触摸点的观点(iOS)

于AppDelegate.m档,创建一个视图控制器 #import "MAYAppDelegate.h" #import "MAYViewController.h" @implementation MAYAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.windo

Oracle创建两表关联查询的视图

在项目开发中,有时候会用到多表查询,有很多种方法,比如关联,比如视图,但对于查询来说,视图查询是最快的,如果你的数据库的字段信息很多,那查询就得整表查,比如两表查询,我们就可以把要的字段抽取出来,放在视图中,这样查询时就只要查询视图中所要的字段,其他的就可以无视.下面我记录一下Oracle创建视图 大多人操作数据库是用Scott权限进行操作数据库,但Scott是没有创建视图的权限的,所以我们要进入管理员System账号,进去给Scott授权一个创建视图权限.进入System后,我们打入以下语句

使用autolayout的NSLayoutConstraint类中的constraintWithItem 、constraintsWithVisualFormat这两个类方法来创建视图并可以实现自动布局

#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self createViewWithConstraintItem]; [self createViewWithConstraint]; } - (void)createViewWithConstraintItem {

比较两个数据库中的视图/存储过程的结构(结构比较,不是功能比较)

CREATE PROC P_COMPDB @DB1 SYSNAME, --第一个库 @DB2 SYSNAME --第二个库 AS EXEC(' SELECT 类型=CASE ISNULL(A.XTYPE,B.XTYPE) WHEN ''V'' THEN ''视图'' ELSE ''存储过程'' END ,匹配情况=CASE WHEN A.NAME IS NULL THEN ''库 ['[email protected]+'] 中无'' WHEN B.NAME IS NULL THEN ''库 [

View Programming Guide for iOS 视图编程指南 - iOS

1 有关 Windows and Views 每个应用都至少有一个 window 和一个 view. 1.1 添加额外的 Window 一般在有外界显示设备的时候才需要添加额外的 window 下面的代码举了一个例子,这里假定对象实现了方法 externalWindow,externalWindow 存储一个 window 的引用 - (void)configureExternalDisplayAndShowWithContent:(UIViewController*)rootVC { // C