iOS development-Advanced ui-gesture Recognition

Source: Internet
Author: User

First, gesture recognizer
0. Open Uiimageview user interaction
self.iconView.userInteractionEnabled = YES;
1. Create a gesture recognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
1.1. Set the gesture Recognizer's properties
Set the user must click several times to trigger a click event
tap.numberoftapsrequired = 2;
Setting the user must have two fingers to tap at the same time to trigger the event
tap.numberoftouchesrequired = 2;

Tap.delegate = self; (and compliance with agreement)

2. Add gesture recognizer to view
[Self.iconview Addgesturerecognizer:tap];

3. Monitor gesture recognizer
[Tap addtarget:self Action: @selector (Tapview)];

The method returns a bool value that determines whether the corresponding Click event
Therefore, if no is returned, the Tapview method is not called.
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *)
Gesturerecognizershouldreceivetouch: (Uitouch *) touch
{
Get the location of the click
Cgpoint point = [Touch LocationInView:touch.view];
if (Point.x > Self.iconView.frame.size.width * 0.5) {
return YES;
}else {
return NO;
}
}

-(void) Tapview
{
NSLog (@ "picture is clicked");
}

Two, long press + swipe
1. Drag a view and drag it into the properties Costomview
2. Long Press Events
-(void) test
{

1. Create a gesture recognizer
Uilongpressgesturerecognizer longpress =
[[Uilongpressgesturerecognizer alloc] init];
1.1 Setting the properties of the long-press gesture recognizer
Longpress.minimumpressduration = 5;
Offset shift that allows the finger to move before the finger presses the post-event response
Longpress.allowablemovement = 10;
2. Add gesture recognizer to view
[Self.customview addgesturerecognizer:longpress];
3. Monitor gesture recognizer
[Longpress addtarget:self Action: @selector (Longpressview)];
}

-(void) Longpressview
{
NSLog (@ "Long press event");
}

3. Swipe Events
-(void) test
{

1. Create a gesture recognizer
Uiswipegesturerecognizer Swipe =
[[Uiswipegesturerecognizer alloc] init];

Set the direction of the swipe (same code to 4 copies in 4 directions)
Swipe.direction = Uiswipegesturerecognizerdireciondown;
2. Add gesture recognizer to view
[Self.customview Addgesturerecognizer:swipe];
3. Monitor gesture recognizer
[Swipe addtarget:self Action: @selector (Swipeview)];
}

-(void) Swipeview
{
NSLog (@ "swipe event");
}

Three, rotate + zoom
1. Drag the picture into the property IconView
2. Rotate (remember to turn multi-touch on the right side of the storyboard)
Uirotationgesturerecognizer *gesture =
[[Uirotationgesturerecognizer alloc] init];
[Self.iconview Addgesturerecognizer:gesture];
Gesture.delegate = self;

[Gesture addtarget:self Action: @selector (Rotationview:)];

-(void) Rotationview: (Uirotationgesturerecognizer *) gesture
{
NSLog (@ "Spin event%.1f", gesture.rotation);
Increments a radian on the incoming transform basis
Self.iconView.transform = Cgaffinetransformmakerotate
(Self.inconView.transform gesture.rotation);
Each time the position of the last gesture as 0 degrees, do not do so, if it is 45 degrees, the next turn 1 degrees
Would add 46 degrees to the original base, and that would be wrong.
gestrue.rotation = 0;
}

3. Scaling Down
Pinch gesture
Uipinchgesturerecognizer *pinch = [[Uipinchgesturerecognizer alloc]init];
[Self.iconview Addgesturerecognizer:pinch];
Pinch.delegate = self;

[Pinch addtarget:self action: @selector (Pinchview)];

-(void) Pinchview: (Uipinchgesturerecognizer *) pinch
{
Self.iconView.transform = Cgaffinetransformscale (self.iconView.transform
, Pinch.scale,pinch.scale);
If the last position is not set to 1.0, it will continue to shrink on the original basis, which is not correct.
Pinch.scale = 1.0;

}

4. Follow the proxy protocol, implement proxy method, make IT support multi-gesture simultaneously
The bool value returned by the method determines whether the view can respond to multiple gestures at the same time
-(BOOL) Gesturerecognizer: (Uigesturerecognizer *) Gesturerecognizer
Shouldrecognizesimultanenouslywithgesturerecognizer:
(Uigesturerecognizer *) Othergesturerecognizer
{
return YES;
}

Iv. Drag and drop
1. Drag the picture into the property Costomview
2. Dragging
Uipangesturerecognizer *pan= [[Uipangesturerecognizer alloc] init];
[Self.costomview Addgesturerecognizer:pan];

[Pan addtarget:self Action: @selector (Panview:)];

-(void) Panview
{
The value returned is the point at which the finger is pressed.
Cgpoint point = [Pan TranslationInView:pan.view];
Cgpoint temp = Self.customView.center;
temp.x + = Point.x;
Temp.y + = Point.y;
Self.customView.center = temp;
To clear it
[Pan.setTranlation:CGPointZero InView:pan.view];

}

V. Basic use of Calayer
Master
Basic properties of Calayer
The relationship between Calayer and UIView
The role of position and Anchorpoint

Calayer
* In iOS, what you can see and touch is basically uiview, like a button, a text label
Sign, a text input box, an icon, and so on, these are UIView
* In fact, UIView can be displayed on the screen entirely because of a layer inside it
* When creating a UIView object, a layer (that is, Calayer object) is automatically created inside UIView,
The Layer property of the UIView can be accessed at this level
@property (Nonatomic,readonly,retain) Calayer *layer;
* When UIView needs to be displayed on the screen, the DrawRect: method is called to draw, and all the content
Draw on your own layer, after the drawing is finished, the system will copy the layer to the screen, so it is finished
The display of the UIView.
* In other words, the UIView itself does not have a display function, it is the inner layer of the display function

Basic use of Calayer
* Some appearance properties of UIView can be easily adjusted by manipulating the Calayer object, such as:
Shadow, fillet size, border width and color
* You can also animate layers to achieve some cool results

Calayer Properties
* Width and height
@property cgrect bounds;
* Position (default midpoint, Anchorpoint decision)
@property cgpoint position; The
* Anchor Point (x, y range is 0-1) determines the meaning of position
@property cgpoint anchorpoint;
* Background color (cgcolorref type)
@property Cgcolorref backgroundcolor;
//So the set color should be XX = [Uicolor Greencolor]. Cgcolor;
* Deformation properties (set Z to not 0, 2d rotation)
@property cgtransform3d transform;
PS: If the assignment ID type is required, then precede the assigned object with (ID).
* Set rounded corners
Cornerradius is the rounded corner of the set primary layer (Calayer)
* Set contents
Layer.contents = (ID) [UIImage imagenamed:@ "Me"]. Cgimage;
contents, for example, if a picture is set, a sublayer is automatically created to show the uiimage so that if the
primary layer has rounded corners, the four corners will come out when the sub-layer is covered. How to solve it?
//sets the portion of the main layer to be cut off (even the shadow is clipped, so if you want to show the shadow and do not want to display the
Four Corners, the picture is processed directly to the fillet)
Self.customView.layer.masksToBounds = YES;
* Set Shadow
Shadowcolor is set shadow color
Shadowoffset is set shadow offset bit (if positive, right and bottom offset)
Shadowopacity is set shadow transparency 0-1 1 completely opaque 0 fully transparent

Vi. custom layer and anchor points, etc.
1. Create a layer
Calayer *layer = [Calayer alloc] init];
Layer.backgroundcolor = [Uicolor Redcolor]. Cgcolor;
Layer.bounds = CGRectMake (0,0,100,100);
Layer.position = Cgpointmake (200,200);
Layer.borderwidth = 10;
Layer.cornerradius = 10;

Add a layer to the interface
[Self.view.layer Addsublayer:layer];

Layer.superlayer;//Gets the parent view of the layer

PS: If a control is a child of another control, the layer in the control is also its child layer

The doubts about Calayer
First of all
Calayer is defined in the Quartzcore framework.
Cgimageref, cgcolorref Two types of data are defined in the Coregraphics framework.
Uicolor, uiimage are defined in the Uikit framework.

Secondly
The Quartzcore framework and coregraphics framework can be used across platforms, both on iOS and Mac OS X
Use, but Uikit Smart is used in iOS
To ensure portability, Quartzcore cannot use Uiimage,uicolor and can only use Cgimageref
, Cgcolorref, so add it later. Cgcolor and so on can be assigned value.


Choice of UIView and Calayer
By Calayer, you can make the same interface effect as Uiimageview.
Since Calayer and UIView can achieve the same display effect, who is the best choice?
In fact, compared to calayer,uiview more than one event-handling function, that is, Calayer can not handle
User's touch event, while UIView can
So if the display needs to interact with the user, use UIView.
If you do not need to interact with the user, you can
Of course, the performance of Calayer is higher because it is less capable of event handling and more lightweight
If the performance requirements of the company is not very harsh, you can use UIView. Or the sudden need to respond to the incident, the egg.
A land.

Positon and Anchorpoint
Calayer has 2 very important properties: position and Anchorpoint

@property cgpoint position;
Used to set the position of the Calayer in the parent layer
In the upper-left corner of the parent layer as the origin point

@property Cgpoint Anchorpoint;
Become anchor points, anchors
Determines which point of the calayer is in the position indicated by the position attribute
The origin of the upper left corner of your own
Its x, Y value range is 0-1, the default value is (0.5,0.5)

iOS development-Advanced ui-gesture Recognition

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.