在RootViewController.m文件中:
- (void)viewDidLoad
{
[super viewDidLoad];
TouchView *view=[[TouchView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
view.multipleTouchEnabled=YES;
view.backgroundColor=[UIColor brownColor];
[self.view addSubview:view];
[view release];
}
在TouchView.m文件中:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
FrameX = self.frame.origin.x;
FrameY = self.frame.origin.y;
FrameW = self.frame.size.width;
FrameH = self.frame.size.height;
NSArray *twoTouches=[touches allObjects];
UITouch *firstTouch=[twoTouches objectAtIndex:0];
UITouch *secondTouch=[twoTouches objectAtIndex:1];
CGPoint prePoint1 = [firstTouch previousLocationInView:self];
CGPoint prePoint2 = [secondTouch previousLocationInView:self];
CGPoint curPoint1 = [firstTouch locationInView:self];
CGPoint curPoint2 = [secondTouch locationInView:self];
initialDistance = [self distanceWithPointA:prePoint1 pointB:prePoint2];//移动之前的距离
currentDistance = [self distanceWithPointA:curPoint1 pointB:curPoint2];//移动之后的距离
float k=currentDistance/initialDistance;
[self setFrame:CGRectMake(FrameX+(FrameW-FrameW*k)/2, FrameY+(FrameH-FrameH*k)/2, FrameW*k, FrameH*k)];
//self.bounds=CGRectMake(0, 0, self.bounds.size.width*k, self.bounds.size.height*k);
}