Using animation for WebView page changes - ios

I've been scouring the web looking for examples of how I could incorporate animation into my UIWebView control but am coming up blank.
I am not picky as to the animation (I can mess with it once I understand how to attach it to the actions), I just want something more than just a stark transition from one web page to another, curled pages, animation from left (old page) to right (new page), whatever will do.
Can someone post an example or direct me to a page that explains how I might do this? I read the docs on CATransition but I still don't see how to incorporate it into my UIWebView navigation.

After some playing around I got the effect I wanted, I probably need to tweak it a bit but here's what I came up with:
Before performing a loadRequest (and I've changed my code for now so that I capture all page changes via shouldStartLoadWithRequest and then set the new page manually using webview loadRequest) I go to a curl animation routine I wrote called animatePage:
- (void) animatePage
{
CATransition *animation = [CATransition animation];
[animation setDelegate:self];
[animation setDuration:1.0f];
animation.startProgress = 0.5;
animation.endProgress = 1;
[animation setTimingFunction:UIViewAnimationCurveEaseInOut];
[animation setType:#"pageCurl"];
//[animation setType:kcat];
[animation setSubtype:kCATransitionMoveIn];
[animation setRemovedOnCompletion:NO];
[animation setFillMode: #"extended"];
[animation setRemovedOnCompletion: NO];
[[myWebView layer] addAnimation:animation forKey:#"WebPageCurl"];
}
So to animate the page changes with a curling upward page animation I basically do:
[self animatePage];
[myWebView loadRequest:myRequest];
I can call the animatePage routine either before or after the loadRequest, it doesn't matter much.
I know this might not be the "right" way to get the job done but it does seem to get the job done. Like I said, I need to dial it in here and there but I'm happy with the result so far.

Using Swift:-
let animation = CATransition()//= [CATransition animation];
animation.delegate = self
animation.duration = 1.0
animation.startProgress = 0.5
animation.endProgress = 1
animation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear)//kCAMediaTimingFunctionEaseInEaseOut)//kCAMediaTimingFunctionEaseOut
animation.type = "pageCurl"
animation.subtype = kCATransitionMoveIn
animation.isRemovedOnCompletion = false
animation.fillMode = "extended"
animation.isRemovedOnCompletion = false
webView?.layer.add(animation, forKey: "WebPageCurl")

Related

transition visual effect from the bottom for table search

Is it possible to controll the transition visual effect from the bottom for table search ?
I want to make this effect :
Finally this is was i did for this :
CATransition *transDown=[CATransition animation];
[transDown setDuration:0.1];
[transDown setType:kCATransitionPush];
[transDown setSubtype:kCATransitionFromTop];
[transDown setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[self.searchDisplayController.searchResultsTableView.layer addAnimation:transDown forKey:nil];
[self.search.layer addAnimation:transDown forKey:nil];

UINavigationController Custom CATransition

I am trying to make a custom transition to push a view controller into the stack.
I dont know if there is an easy way to "lock" the navigationBar on its position and make the transition under the navigationBar.
I am using the following code:
CATransition *transition = [CATransition animation];
transition.duration = 0.3f;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromBottom;
[self.navigationController.view.layer addAnimation:transition forKey:nil];
First I tried the kCATransitionPush, but I could see the navigationBar moving down... so I tested kCATransitionMoveIn and it feels a bit better, but still not the way I wanted...
Is there a simple way to achieve this?
What you're doing is not how you do custom navigation controller transitions. You have to use the iOS 7 custom animator protocols (e.g. UIViewControllerAnimatedTransitioning).

How to use AVFoundation to apply a different text for different segments of a video

I'm trying to watermark a video's elapsed time onto the video frames. But I could only render a static text using AVVideoCompositionCoreAnimationTool and CALayer into for the entire video duration.
How can I apply change the text that is overlaid on the video every second?
Similar questions in Stack Overflow hasn't been answered too
AVFoundation how to add watermark with specified time and
duration
How to watermark your video with different images and different
CMTimes using
AVFoundation
You can refer
http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos
you just need to CABasicAnimation and add your title layer to it. Just check it out to changing the needed parameters its working fine. I Have done it in my one of project
CATextLayer *title = [CATextLayer layer];
// code for title layer setting
// code for the opacity animation which will remove the text
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"opacity"];
[animation setDuration:0];
[animation setFromValue:[NSNumber numberWithFloat:1.0]];
[animation setToValue:[NSNumber numberWithFloat:0.0]];
[animation setBeginTime:1];
[animation setRemovedOnCompletion:NO];
[animation setFillMode:kCAFillModeForwards];
[title addAnimation:animation forKey:#"animateOpacity"];

UIGestureRecognizer on transformed layer

I'm working on and iPhone app, and have an issue with a gesture recognizer.
I've added an UITapGestureRecognizer on a view, then I transform the layer related to this view with CABasicAnimation. After this transformation, the gesture recognizer only works in the area occupied by the view before the transformation.
Hope this little description of my problem is understandable..
Here is some code :
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(myViewTapped:)];
[self.myView addGestureRecognizer:tapGestureRecognizer];
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:#"position.y"];
[animation setFromValue:[NSNumber numberWithFloat:0]];
[animation setToValue:[NSNumber numberWithFloat: - 100]];
[animation setDuration:.3];
[animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:.55 :-0.25 :.30 :1.4]];
animation.additive = YES;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[self.myView.layer addAnimation:animation forKey:nil];
How can I handle this issue ?
Thanks !
You are animating only the graphical part of the view (the CALayer), not the part responsable for user interaction (the UIView itself).
Your code move the layer and made it been drown elsewhere, but don't change the frame (or the bound+center).
You have 3 option (well maybe more, just I can think of these 3):
1) use the UIView-based animations [UIView animation...]
2) struct with you code but also relocare the view after the animation take place (but this may rise issues cause your layer will be moved also).
3) use your animation but put the gesture recognizer on a parent (bigger) view and then check the events there...

CATransition Push transition replacing content early

I have a simple animation setup in a Mac App where a page control swaps out some views. I am very close to getting the paging-like animation I want, but there is a slight problem. The paging works and the new page animates into place correctly, but the new page also replaces the initial page before the animation. I wish to ultimately have the view that is being pushed out remain the same instead of changing to the new page early. Here is a video showing the problem, slowed down to a 3 second animation duration. Here is the code:
UASourceView *previousSourceView = [self.sourceViewContainer.subviews objectAtIndex:0];
NSInteger previousIndex = [self.sourceViews indexOfObjectIdenticalTo:previousSourceView];
CATransition *pushTransition = [CATransition animation];
[pushTransition setType:kCATransitionPush];
[pushTransition setSubtype:(previousIndex < index) ? kCATransitionFromRight : kCATransitionFromLeft];
[pushTransition setDuration:PANEL_PAGE_SWIPE_ANIMATION_DURATION];
[pushTransition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[pushTransition setRemovedOnCompletion:YES];
[CATransaction begin];
[self.sourceViewContainer replaceSubview:previousSourceView with:sourceView];
[CATransaction commit];
[sourceView.layer addAnimation:pushTransition forKey:#"push"];
There is a fixed container view self.sourceViewContainer that gets a new subview replaced on every animation. The problem, as seen above is that the previousSourceView gets replaced immediately by the sourceView and gets pushed in as well. Please help me stop the immediate replacement. Where am I going wrong?
*Note, iOS tag is added because this code is platform independent.
I solved similar issues using CAAnimationBlocks (this is my fork of the original, which the author has moved in a direction I didn't much care for).
The idea is to set removedOnCompletion to NO and fillMode to kCAFillModeForwards and perform the actual switch (without animation) in the completion block. This has been tested under OSX only (you might need a different solution for iOS, which already supports completion blocks).
Here's an example usage from my code, which moves a piece on a chess board, and back again:
CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:#"position"];
moveAnimation.fromValue = [NSValue valueWithPoint:[self _pointForSquare:move.from()]];
moveAnimation.toValue = [NSValue valueWithPoint:[self _pointForSquare:move.to()]];
moveAnimation.duration = _animateSpeed / 1000.0;
moveAnimation.autoreverses = YES;
moveAnimation.removedOnCompletion = NO;
moveAnimation.fillMode = kCAFillModeForwards;
moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
moveAnimation.completion = ^(BOOL finished)
{
[pieceLayer removeAnimationForKey:#"movePiece"];
[self _setPieceLayer:pieceLayer toPiece:piece];
[pieceLayer setNeedsDisplay];
};
[pieceLayer addAnimation:moveAnimation forKey:#"movePiece"];

Resources