Ios-runtime intercepts touch events, displays user touch effects, and ios-runtimetouch

Source: Internet
Author: User

Ios-runtime intercepts touch events, displays user touch effects, and ios-runtimetouch

[Display effect description]

When you screen recording an app, you can view the touch effect of the shot on the app to see the specific clicks. This makes it easy and you do not need to post the video.

Here, we simply use runtime to achieve this effect. You don't need to modify any code. You just need to import the UIWindow + Touch. m classification into the project.

The results are as follows:

    

 

[Implementation]

1. To intercept all click events, obtain the position of the click relative to the screen. In ios, click events in event distribution should be like this: the first click event in the system-> distributed to application-> distributed to window-> subview in window layer traversal.

2. It is only a small function. To make this function controllable, do not change the original code. Use the runtime Exchange System Method to add your own code when distributing events.

3. After obtaining the position of the touch point, you can create a view to display some animation effects.

As you can see from the above 1st, the most reliable interception of events in the application, because the first distribution of the system is to give it, the operations here are all UI-related, therefore, the UI-related events are placed on the window for processing and comparison.

 

[Code]

Create a new UIWindow + Touch category. swap the sendEvent method in the m file, and add the code that you have processed. The main reason is to obtain the position of x and y, add a view, and add an animation, in the end, the above results are achieved. The UIWindow + Touch. m code is as follows.

# Import "UIWindow + Touch. h" # import <objc/runtime. h> @ implementation UIWindow (Touch) + (void) load {
// Exchange Method m1 = class_getInstanceMethod ([self class], @ selector (sendEvent :)); Method m2 = class_getInstanceMethod ([self class], @ selector (ljs_sendEvent :)); method_exchangeImplementations (m1, m2);}-(void) ljs_sendEvent :( UIEvent *) event {[self ljs_sendEvent: event];
// Process the click event [self dealTouch: event];}-(void) dealTouch :( UIEvent *) event {UITouch * touch = event. allTouches. anyObject; if (touch. phase = UITouchPhaseEnded) {return;} static CGFloat width = 20; if (event. type = UIEventTypeTouches) {CGPoint point = [event. allTouches. anyObject locationInView: self]; CGFloat oringX = point. x-width/2; CGFloat oringY = point. y-width/2; CGRect rect = CGRectMake (oringX, oringY, width, width); UIView * blackV = [[UIView alloc] initWithFrame: rect]; blackV. alpha = 0.3; blackV. layer. cornerRadius = width/2; blackV. backgroundColor = [UIColor purpleColor]; [self addSubview: blackV]; [self bringSubviewToFront: blackV]; // set the animation CABasicAnimation * animation = [CABasicAnimation placement: @ "transform. scale "]; animation. delegate = self; animation. duration = 0.3; animation. fromValue = @ 1; animation. toValue = @ 2; animation. fillMode = @ "backwards"; animation. removedOnCompletion = YES; [blackV. layer addAnimation: animation forKey: @ "an1"]; dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (0.27 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {[blackV removeFromSuperview] ;}}}@ end

 

Related Article

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.