For core animation, whether it's an explicit or an implicit animation, the frame is set immediately, such as moving an animation to a uiview, although it looks like the frame is constantly changing, but when its frame is the final value, in this case, Even if this uiview is an example of UIButton, it triggers the range of touch events to be the place of the final frame. For example, a button's frame is (0,0,100,100), to move it from 0, 0 to 200,200, in this case:
- If you are using an explicit animation (Cakeyframeanimation and Cabasicanimation) that is animated by specifying path or values, its frame does not change, touch range or (0,0,100,100) Within this range
- If you are using an implicit animation that is animated by setting a frame, then its touch range is (200,200,100,100) within this range
If we want to add a touch event to a view in a mobile animation, we need to use hittest later. Let's first create an animated 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;
This is only anchorpoint important, because anchorpoint can affect the value of position, for layer, frame is abstract, only bounds and position is real, and set the frame and set the Anchorpoint in different order, you start to see different results:
First set Anchorpoint start to get the correct results, that is, position is 0, 0, if the setting bounds is not so troublesome, the order does not matter. The reason to set Anchorpoint is because the cakeyframeanimation will be animated with position, and if Anchorpoint is the default (that is, 0.5,0.5), there will be an X-, y-axis offset of half, A graph explaining the value of Anchorpoint:
Position is to anchorpoint as the origin, the default of the origin in the center, the natural will appear in the X, Y axis offset, in fact, as long as I in the animation will position do not set the offset anchorpoint, but I think the animation from 0 to the endpoint more intuitive, So that's why Anchorpoint is set up. Finally, a weak property is used to refer to the animation layer and add the layer to the self.view.layer. Next is the animation section:
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 the animation is done with cabasicanimation, then replace Setvalues,timingfunction with Fromvalue and tovalue directly with the linear, do not do other transformations, about this property preset value, I mentioned in another blog post.
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");} }
I mentioned at the very beginning that the process of animation just seems to be dynamic and its internal values are fixed, in which case the layer interior will show the animation process by copying a copy of the current layer. And we can get a copy of this copy by accessing the Presentationlayer property of the layer, through presentationlayer we can know where the current animation is on the screen, Then directly through the hittest to determine whether it is a valid click.