IOS adds touch events to Views in Mobile animations,

Source: Internet
Author: User

IOS adds touch events to Views in Mobile animations,
For Core Animation, whether it is an explicit or implicit Animation, frame setting is set immediately. For example, if you want to perform a mobile Animation for a UIView, although the frame is constantly changing, however, its frame is now the final value. In this case, even if the UIView is a UIButton instance, the range of the touch event triggered is still the final frame. For example, if the frame of a Button is (100,100, 200,200), you need to move it from to. In this case:
If we want to add touch events for views in a mobile animation, we need to use hitTest later. First, create an animation Layer:

CGSize layerSize = CGSizeMake(100, 100);CALayer *movingLayer = [CALayer layer];movingLayer.bounds = CGRectMake(0, 0, layerSize.width, layerSize.height);[movingLayer setBackgroundColor:[UIColor orangeColor].CGColor];movingLayer.anchorPoint = CGPointMake(0, 0);[self.view.layer addSublayer:movingLayer];self.movingLayer = movingLayer;
Here, only anchorPoint is important, because anchorPoint can affect the value of position. For Layer, frame is abstract, and only bounds and position exist, in addition, the sequence of frame setting and anchorPoint setting is different, and the starting result is also different:
Set anchorPoint on the left and frame on the right. You can see that if anchorPoint is set first, the right result is displayed at the beginning, that is, the position is 0, 0. However, if bounds is set, you don't have to worry about the order. The reason why I want to set anchorPoint here is that the position animation will be used in the subsequent CAKeyframeAnimation. If anchorPoint is the default value (0.5, 0.5, an offset half of the x and Y axes will appear in the animation, as shown in the following figure:
The following figure illustrates the value of anchorPoint:
Position is based on anchorPoint. By default, this origin is in the center, and the offset of the x and Y axes will naturally appear. In fact, as long as I make an offset of the position in the animation, there is no need to set anchorPoint, however, I think the animation is more intuitive from 0 to the end, so this is why I set anchorPoint. Finally, use a weak attribute to reference the animation Layer and add the Layer to self. view. layer. Next is the animation part:
CAKeyframeAnimation *moveLayerAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];//moveLayerAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];//moveLayerAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(320 - self.movingLayer.bounds.size.width, 0)];moveLayerAnimation.values = @[[NSValue valueWithCGPoint:CGPointMake(0, 0)],                              [NSValue valueWithCGPoint:CGPointMake(320 - self.movingLayer.bounds.size.width, 0)]];moveLayerAnimation.duration = 2.0;moveLayerAnimation.autoreverses = YES;moveLayerAnimation.repeatCount = INFINITY;moveLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];[self.movingLayer addAnimation:moveLayerAnimation forKey:@"move"];
If CABasicAnimation is used for animation, fromValue and toValue are used to replace setValues. timingFunction uses linearity directly without other transformations. For the preset values of this attribute, I mentioned it in another blog.
Next, add Gesture Recognition for self. view:
........    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];    [self.view addGestureRecognizer:self.tapGesture];}-(void)click:(UITapGestureRecognizer *)tapGesture {    CGPoint touchPoint = [tapGesture locationInView:self.view];    if ([self.movingLayer.presentationLayer hitTest:touchPoint]) {        NSLog(@"presentationLayer");    }}
As I mentioned at the beginning, the animation process only seems to be dynamically transformed, and its internal values are fixed. Next, let's briefly describe Core Animation. Core Animation uses three sets for Animation: Layer Tree, Presentation Tree, and Render Tree. Layer Tree is used to store the final value of a Layer, that is, without considering the occurrence of an animation. You only need to assign values to a Layer, such as backgroundColor, position, and alpha, the values stored in the Layer Tree change immediately. The Presentation Tree is used to store all the values to be displayed during the animation process, which is opposite to the Layer Tree. The Render Tree is used to Render the values in the Presentation Tree, it is also the only link for interaction with Core Animation. This process is hidden by the system and we can't worry about it. This means that both the Layer Tree and the Presentation Tree are equivalent to model objects and only store the Layer state. When we want to read the state of the animation in progress, we can call the presentationLayer attribute of the layer, this attribute returns the Layer representing the current dynamic state from the Presentation Tree. Then, you can call the hitTest method of the Layer to determine whether a click is valid.
Download Demo


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.