Design principle of gesture diagram (1)

Source: Internet
Author: User

VIEWCONTROLLER.M/* Gestures:
Uiresponder: is a responder (the speaker) used to respond to certain events that the user touches the screen
Finger Start touch Screen call
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Nullable uievent *) event;
Time stamp
Click Count
You can get the location of the click View *******
-(Cgpoint) Locationinview: (Nullable UIView *) view;

Finger touch screen Start moving
-(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (Nullable uievent *) event;
Call when your finger leaves the screen
-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Nullable uievent *) event;
Called when the finger is removed from the touch by an external event
-(void) touchescancelled: (Nullable nsset<uitouch *> *) touches withevent: (Nullable uievent *) event;

Gesture does not invoke check when setting the background color


Gestures are divided into six major gestures:
All six gestures are inherited from Uigesturerecognizer.
1. Click UITapGestureRecognizer
2, Long press Uilongpressgesturerecognizer
3, drag Uipangesturerecognizer
4, kneading Uipinchgesturerecognizer
5. Swipe Uiswipegesturerecognizer
6. Rotating Uirotationgesturerecognizer

Touchesbegan moved end (mouse start, move, end)
A touch event can be obtained through touches
Uitouch *touch = [touches anyobjects];
The position of the point where the touch can be obtained by Uitouch

*/
#import "ViewController.h"

@interface Viewcontroller ()
{

Uiimageview *imageview;

}

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];

Self.view.backgroundColor = [Uicolor Whitecolor];
ImageView = [[Uiimageview alloc]initwithframe:cgrectmake (0, 0, 100, 100)];
Imageview.image = [UIImage imagenamed:@ "Fly_1"];
[Self.view Addsubview:imageview];

Imageview.animationimages = @[[uiimage imagenamed:@ "fly_1"],[uiimage imagenamed:@ "fly_2"],[UIImage imageNamed:@ "Fly _3 "],[uiimage imagenamed:@" Fly_4 "];
Imageview.animationduration = 1;


Uigesturerecognizer
Initialize gestures
-(Instancetype) Initwithtarget: (nullable ID) Target action: (nullable SEL) action;
How to add gestures in UIView
Addgesturerecognizer:

You can find the clicked view by using the View property inside the gesture.
Locationinview: Find the location of the click

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (Actionoftap:)];

Set the number of clicks on a gesture click (to move)
tap.numberoftapsrequired = 2;

Set the number of tap fingers
tap.numberoftouchesrequired = 2;
[Self.view Addgesturerecognizer:tap];

Long-Press gestures
Uilongpressgesturerecognizer *longpress = [[Uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector (actionoflongpress:)];
Set long-press time
Longpress.minimumpressduration = 3;
[Self.view addgesturerecognizer:longpress];

}
/*
typedef ns_enum (Nsinteger, uigesturerecognizerstate) {
Uigesturerecognizerstatepossible, default state Uigesturerecognizerstatebegan, gesture start
Uigesturerecognizerstatechanged, the gesture changed.
uigesturerecognizerstateended, gesture over.
uigesturerecognizerstatecancelled, gesture canceled state
Uigesturerecognizerstatefailed, gesture failed state
};
*/
To get these states, you can use state-

Tap gestures
-(void) Actionoftap: (UITapGestureRecognizer *) sender{

UIView *view = Sender.view;
View.backgroundcolor = [Uicolor Redcolor];

Cgpoint point = [Sender LocationInView:self.view];
Imageview.center = point; (deleted)

The state of the view from the original state to the animations within 2 seconds after execution
Motion tween (which only moves the view as the cursor clicks, and does not change his size shape)
[UIView Animatewithduration:2 animations:^{
Imageview.center = point;
//    }];

You can not only move the view as the cursor moves, but also change the size of the shape after the move and the end of the move.
[UIView Animatewithduration:2 animations:^{
Imageview.center = point;
Imageview.bounds = CGRectMake (0, 0, 200, 200);
} completion:^ (BOOL finished) {
Called after the animation is complete
[UIView Animatewithduration:2 animations:^{

Imageview.bounds = CGRectMake (0, 0, 100, 100);
}];//will be executed in 2 seconds from the original default state to the animations state.

}];
/*
UIView Animation
The system helped us encapsulate the core animation.

Animate duration (duratation) and animations
[UIView animatewithduration:1 animations:^{

}];

Completion method that is called after the animation is completed
[UIView animatewithduration:1 animations:^{

} completion:^ (BOOL finished) {

}];

*/
}
Long press the Implementation method
-(void) Actionoflongpress: (Uilongpressgesturerecognizer *) sender{

if (sender.state = = Uigesturerecognizerstatebegan) {
Imageview.bounds = CGRectMake (0, 0, 200, 200);
NSLog (@ "magnified ~");
}
if (sender.state = = uigesturerecognizerstateended) {
Imageview.bounds = CGRectMake (0, 0, 100, 100);
NSLog (@ "restored ~");
}

}
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Nullable uievent *) even
//{
Locationinview:uitouch find a point in the click View
Anyobject can get a touch event in the touches collection
Uitouch *touch = [touches anyobject];
Find the touch point above the touch view
Cgpoint point = [Touch LocationInView:self.view];
NSLog (@ "x:%f y:%f", POINT.X,POINT.Y);
//
Let the picture move as the mouse cursor moves
Imageview.center = point;
//
[ImageView startanimating];
//
//}
//
Finger touch screen Start moving
-(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (Nullable uievent *) event
//{
Uitouch *touch = [touches anyobject];
Cgpoint point = [Touch LocationInView:self.view];
Imageview.center = point;
//
//
//}
Call when your finger leaves the screen
-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Nullable uievent *) event
//{
//
[ImageView stopanimating];
//}



-(void) didreceivememorywarning {
[Super didreceivememorywarning];

/*
All views inherit Uiresponder->uituchbegin moved end when user taps screen trigger
Uituchbegin moved End

1. Click to get Touch events
Uitouch *touch = [touches anyobject]
2. Get the location of the user click
Cgpoint point = [Touch locationinview:xx];

Uigesturerecognizer:
All gestures are initialized by the
-(Instancetype) Initwithtarget: (nullable ID) Target action: (Nullable SEL) action

Add gestures to the view
[xx view addgesturerecognizer:xx gesture];

A view that can be touched by gestures
View this property gets

Get the location of the click View
Cgpoint point = [xx gesture locationinview:xx view];


1. Tap gestures
UITapGestureRecognizer

Numberoftapsrequired: Set the number of clicks
Numberoftouchesrequired: Set the number of clicks on your finger

2, Uilongpressgesturerecognizer
Minimumpressduration: Setting the minimum duration for long presses will trigger events after the duration of the long press

UIView Animation
Animate the properties of a view in an animation
[UIView animatewithduration: Time animations:^{
The property you want to change results in an animated effect that finishes after the set time
}];


[UIView animatewithduration: Time animations:^{
The property you want to change results in an animated effect that finishes after the set time
} completion^ (BOOL) {
Called when the animation finishes executing
}];
*/
}

@end

Design principle of gesture diagram (1)

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.