通过两个触摸点实现视图的缩放(iOS)
来源:互联网 发布:淘宝微星旗舰店可信吗 编辑:程序博客网 时间:2024/05/12 00:22
在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);
}
- 通过两个触摸点实现视图的缩放(iOS)
- 视图响应触摸事件的缩放效果
- Xamarin.iOS滑动视图上图片(视图)的缩放(双击手势实现缩放)
- IOS 触摸获取坐标点、缩放图片实例
- ios实现视图的拖拽、缩放、旋转
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- 在Unity中通过触摸屏幕实现模型的旋转和缩放
- android应用之多点触摸实现图片缩放
- iOS开发 单手使触摸的视图旋转
- NGUI的双点触摸缩放UI(模拟Camera变焦)
- IOS获取当前手指触摸的点
- ios视图缩放动画
- ios视图缩放动画
- IOS之多点触摸
- IOS中动画的实现:以及视图的移动、缩放和旋转
- IOS中动画的实现:以及视图的移动、缩放和旋转
- iOS中动画的实现:以及视图的移动、缩放和旋转
- 【Andorid开发框架学习】之Mina开发之服务器开发
- cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第八部---怪物出场
- 【Android小项目】找不同,改编自"寻找房祖名"的一款开源小应用。
- Eclipse下配置CDT和MinGW终极方法。
- LINUX设备驱动程序笔记(一)设备驱动程序简介
- 通过两个触摸点实现视图的缩放(iOS)
- oracle表结构导出导入
- LINUX设备驱动程序笔记(二)构造和运行模块
- test HelloWorld
- C# using 三种使用方式
- C++单例模式
- 基于fl2440内核linux-3.0移植----添加adc驱动
- LINUX设备驱动程序笔记(三)字符设备驱动程序
- NYOJ 5 Binary String Matching