IOS UI View Move and zoom

Source: Internet
Author: User

Moving ideas:

Set a private property to store a point, record the position of the touch point at the beginning of the touch, record the point moved to in the touch action movement, find the change in the Y axis of the x axis of the two points, assign the center point B of the original view to the new point, and the new point x, y plus the change to the new point, Move the view through the changes in the center point

@interface Ydview ()

@property (nonatomic, assign) Cgpoint beginpoint;
@end

@implementation Ydview

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
Uitouch * Touch = [touches anyobject];
Self.beginpoint = [Touch locationinview:self];
}
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
Uitouch * Touch = [touches anyobject];
Cgpoint point = [Touch locationinview:self];
CGFloat Changex = point.x-self.beginpoint.x;
CGFloat changey = Point.y-self.beginpoint.y;
Cgpoint Center = self.center;
Center.x + = Changex;
Center.y + = Changey;
Self.center = center;


}

@end

Scaling ideas:

You need to turn on multi-touch first (it's off by default).

After calculating the zoom A of the view, the bounts * A of the view is given a new bounds, and there is a bounds change to control the view; (There is no way to use frame, why I can't say (the upper left corner will not change), which can tell me the trouble.

Figure out the scale of the view a: Defines the distance between two points (two fingers) of a private property. The ratio of the new distance to the old distance is the scale of the view (the ratio of two finger scaling before and after zooming)

#import "SuofangView.h"

@interface Suofangview ()
@property (nonatomic, assign) CGFloat begindistance;
@end
@implementation Suofangview

-(Instancetype) initWithFrame: (CGRect) frame

{
self = [super Initwithframe:frame];
if (self) {
self.multipletouchenabled = yes;//need to turn on multi-touch first
}
return self;
}
-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) event
{
Uitouch * Touch1 = [[Touches allobjects] lastobject];//) touches is a collection type
Uitouch * TOUCH2 = [[Touches allobjects] firstobject];
Cgpoint P1 = [Touch1 locationinview:self];
Cgpoint P2 = [Touch2 locationinview:self];
Self.begindistance = [self getdistance:p1 p2:p2];
}

-(CGFloat) Getdistance: (Cgpoint) P1
P2: (cgpoint) p2//two point distance
{
return sqrt (Pow ((p1.x-p2.x), 2) + POW (P1.Y-P2.Y, 2));
}
-(void) touchesmoved: (Nsset *) touches withevent: (Uievent *) event
{
Uitouch * Touch1 = [[Touches allobjects] lastobject];
Uitouch * TOUCH2 = [[Touches allobjects] firstobject];
Cgpoint P1 = [Touch1 locationinview:self];
Cgpoint P2 = [Touch2 locationinview:self];
CGFloat distance = [self getdistance:p1 p2:p2];

CGFloat scale = distance/self.begindistance;
CGRect newbount = self.bounds;
Newbount.size.height *= Scale;
Newbount.size.width *= Scale;
Self.bounds = newbount;//assigns the new bounds to Self.bounds
Self.begindistance = distance;//The new distance to self.begindistance, without this sentence, the scale will be wrong, try it yourself (dividend will never change.)
}
@end

IOS UI View Move and zoom

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.