Zoom (IOS) for views with two touch points

Source: Internet
Author: User

In the APPDELEGATE.M file, create the View controller

#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;
}

Create a View controller Mayviewcontroller, in the View controller mayviewcontroller.m file, create a view


#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];

}

Create a new view class touch, add on the view controller, implement the Zoom function in the TOUCH.M file
#import "TOUCH.H"

@implementation Touch

-(ID) initWithFrame: (CGRect) frame
{
self = [super Initwithframe:frame];
if (self) {
Initialization code
Set whether multi-touch is supported (yes support, no not supported)
self.multipletouchenabled = YES;
}
return self;
}
Calculate the distance between two points
-(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
}
Set Touch points
Nsarray *array = [touches allobjects];
Uitouch *touch1 = [array firstobject];
Uitouch *TOUCH2 = [array lastobject];
Get the touch point before moving
Cgpoint firstpreviouspoint = [Touch1 previouslocationinview:self];
Cgpoint secondpreviouspoint = [Touch2 previouslocationinview:self];

Gets and moves the touch point after
Cgpoint firstcurrentpoint = [Touch1 locationinview:self];
Cgpoint secondcurrentpoint = [Touch2 locationinview:self];

Gets the distance between the two points before and after the move
CGFloat previousdistance = [self distance:firstpreviouspoint point2:secondpreviouspoint];
CGFloat currentdistance = [self distance:firstcurrentpoint point2:secondcurrentpoint];

Get zoom ratio
CGFloat scanl = currentdistance/previousdistance;

Get the Zoomed view size

Self.bounds = CGRectMake (0, 0, self.bounds.size.width * scanl, Self.bounds.size.height * scanl);
}


Zoom (IOS) for views with two touch points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.