要缩小通过两个触摸点的观点(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);
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
最新文章
- Win 10 文件浏览器无法打开
- moffiestyle
- PPC MPC85xx e500学习笔记
- git中通过实际操作来了解常用命令
- Highcharts导出gb2312乱码问题
- codevs3872 邮递员送信(SPFA)
- BitMask 使用参考
- Delphi richedit获取选中文字
- struts validate
- 【git】切换分支获取代码
- javascript GB2312转UTF8
- JDBC连接Oracle数据库时出现的ORA-12505错误及解决办法
- SQLite基础回顾
- org.springframework.web.servlet.PageNotFound
- linux java 和jmeter 环境变量配置文件笔记(原)
- 收藏一个带动画效果的ScrollViewer以及ScrollBar的模板
- MFC头文件
- 第14月第30天 svn 撤销ignore revert
- 连接oracle数据库报错:TNS-12516 TNS:listener could not find available handler with matching protocol stack解决方法
- 实战分析: MySQL字符集