//
// CLView.m
// Homework_UIEvent
//
// Created by lanouhn on 14-8-26.
// Copyright (c) 2014年 [email protected] 陳聰雷. All rights reserved.
//
#import "CLView.h"
@interface CLView ()
//{
// CGPoint _firstPreviousLocation;
// CGPoint _secondPreviousLocation;
//}
@end
@implementation CLView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.multipleTouchEnabled = YES;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
static int i = 1;
//雙擊放大, 再次雙擊恢復
if (1 == touches.count && 2 == [[touches anyObject] tapCount]) {
if (1 == i % 2) {
self.bounds = CGRectMake(0, 0, self.bounds.size.width * 3.0, self.bounds.size.height * 3.0);
} else {
self.bounds = CGRectMake(0, 0, self.bounds.size.width / 3.0, self.bounds.size.height / 3.0);
}
i++;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//獲取兩個點移動之後的位置
NSArray *allTouches = [touches allObjects];
UITouch *firstTouch = [allTouches firstObject];
UITouch *secondTouch = [allTouches lastObject];
//獲取兩個手指之前的位置
CGPoint firstPreviousLocation = [firstTouch previousLocationInView:self];
CGPoint secondPreviousLocation = [secondTouch previousLocationInView:self];
//獲取兩個手指當前位置
CGPoint firstCurrentLocation = [firstTouch locationInView:self];
CGPoint secondCurrentLocation = [secondTouch locationInView:self];
//若只有一個手指觸摸屏幕並移動時, view同樣移動
if (1 == [touches count]) {
self.center = CGPointMake(self.center.x + firstCurrentLocation.x - firstPreviousLocation.x, self.center.y + firstCurrentLocation.y - firstPreviousLocation.y);
return;
}
//獲取之前兩個點的距離
CGFloat previousDistance = [self distanceOfPoint1:firstPreviousLocation point2:secondPreviousLocation];
//獲取當前兩個點的距離
CGFloat currentDistance = [self distanceOfPoint1:firstCurrentLocation point2:secondCurrentLocation];
// NSLog(@"%g", previousDistance);
//求變化的比例
CGFloat scale = currentDistance / previousDistance;
//修改視圖的大小, 按比例縮放
self.bounds = CGRectMake(0, 0, self.bounds.size.width * scale, self.bounds.size.height * scale);
}
//計算兩個點的距離
- (CGFloat)distanceOfPoint1:(CGPoint)point1 point2:(CGPoint)point2
{
CGFloat dx = point1.x - point2.x;
CGFloat dy = point1.y - point2.y;
return sqrt(pow(dx, 2) + pow(dy, 2));
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
捏合視圖變化(視圖的縮放), 單擊隨着鼠標移動, 雙擊放大視圖,再雙擊還原視圖
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.