Present modal controller from specified point - ios

I have buttons on some viewController view. touchUpInside: methods present modal view controllers. I want to present them from center of tapped button frame. How can I achieve this result?
I tried:
webController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, self.buuton.center.x, self.button.center.y);
[self presentViewController:webController animated:NO completion:^{
// some code
}];
but it damaged my UI. I also tried to use [UIView beginAnimation], no success.

I have achieved something similar with a trick, adding destvc.view as subview to sourcevc.view, see below my functions to scale in and scale out.
You can use the same trick only as visual effect, and present your vc at the end, without animation:
+(void) animWithAlpha:(float)alpha view:(UIView*)view{
[UIView animateWithDuration:0.15
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
view.alpha = alpha;
}
completion:^(BOOL fin) {}];
}
+(void) zoomIN:(UIViewController*)sourceViewController destination:(UIViewController*)destinationViewController fromPoint:(CGPoint)point{
[sourceViewController.view addSubview:destinationViewController.view];
[destinationViewController.view setFrame:sourceViewController.view.window.frame];
[destinationViewController.view setTransform:CGAffineTransformMakeScale(0.15, 0.1)];
destinationViewController.view.center = point;
sourceViewController.view.userInteractionEnabled=NO;
destinationViewController.view.alpha = 0.5;
[self animWithAlpha:1 view:destinationViewController.view];
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[destinationViewController.view setTransform:CGAffineTransformMakeScale(1, 1)];
destinationViewController.view.center = sourceViewController.view.center;
}
completion:^(BOOL finished){
if(finished){
[destinationViewController.view setTransform:CGAffineTransformMakeScale(1, 1)];
destinationViewController.view.center = sourceViewController.view.center;
sourceViewController.view.userInteractionEnabled=YES;
}
}];
}
+(void) zoomOUT:(UIViewController*)sourceViewController destination:(UIView*)destination toPoint:(CGPoint)point{
[sourceViewController.view setTransform:CGAffineTransformMakeScale(1.0, 1.0)];
[sourceViewController.parentViewController.view setUserInteractionEnabled:NO];
[destination setUserInteractionEnabled:NO];
[UIView animateWithDuration:0.4
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[sourceViewController.view setTransform:CGAffineTransformMakeScale(0.01, 0.01)];
[sourceViewController.view setAlpha:1.0];
sourceViewController.view.center = point;
}
completion:^(BOOL finished){
if(finished){
[sourceViewController.view setTransform:CGAffineTransformMakeScale(1.0, 1.0)];
[sourceViewController.view removeFromSuperview];
sourceViewController.view.center = point;
[destination setUserInteractionEnabled:YES];
}
}

You have to do that through addSubview: method and doing something like:
- (void) appear: (UIView *) view fromPoint: (CGPoint) point
{
[view setBounds:self.view.bounds];
[view setCenter:point];
[view setAlpha:0.0];
[self addSubview:view];
[view setTransform:CGAffineTransformMakeScale(0, 0)];
[UIView animateWithDuration:0.3 animations:^{
[view setCenter:self.view.center];
[view setTransform:CGAffineTransformIdentity];
[view setAlpha:1.0];
}];
}
You have to create the view before call the method.
Good Luck!

Related

Continous fade in and fade out

Im trying to fade in and out of a light node I have created I just dont know where to call the function accordingly so it will continously fade in and out. Never tried animating anything here is what I have.
- (void)fadeOutIn:(UIView *)view duration:(NSTimeInterval)duration
{
[self enumerateChildNodesWithName:#"//*" usingBlock:^(SKNode *node, BOOL *stop) {
if ([node.name isEqualToString:#"light1"]) {
[UIView animateKeyframesWithDuration:5 delay:2 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationCurveEaseInOut animations:^{
node.alpha = 0;
} completion:^(BOOL finished) {
}];
}
}];
}
where should I call this function?that is fades in and out continously?
Should it be called within an action?
Thank You
Use this for Continue fade in out.
-(void)viewDidAppear:(BOOL)animated{
[UIView animateKeyframesWithDuration:1 delay:0 options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction animations:^{
node.alpha = 0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 animations:^{
node.alpha = 1;
} completion:nil];
}];
}
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[objAnimate setAlpha:1.0];
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
[objAnimate setAlpha:0.0];
} completion:nil];
}
This will create continuous fade in and out for your object when it appears. If you want to create programmatically a custom object and then use this code then do it after your custom object is added in your view as below (example):
objAnimate = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 200, 40)];
objAnimate.backgroundColor = [UIColor lightGrayColor];
[objAnimate setAlpha:1.0];
[self.view addSubview:objAnimate];
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear | UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse animations:^{
[objAnimate setAlpha:0.0];
} completion:nil];
The view starts animating as soon as its added in your view.

UIView setHidden:NO like dropdown

I have a UIView which is initially hidden. I need to setHidden:NO (visible) with dropdown effect...
There's my simple code without effect
-(IBAction)btnAbrirDestaquesClick:(id)sender {
[self.viewDestaques setHidden:NO];
}
A simpler alternative to UIDynamicAnimator in iOS 7 is Spring Animation (a new and powerful UIView block animation), which can give you nice bouncing effect with damping and velocity:
[UIView animateWithDuration:duration
delay:delay
usingSpringWithDamping:damping
initialSpringVelocity:velocity
options:options animations:^{
//Animations
[self.viewDestaques setHidden:NO];
}
completion:^(BOOL finished) {
//Completion Block
}];
If you simply want to animate it try something like this:
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.viewDestaques.frame = CGRectMake(0, 0, 320,30);
} completion:^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.viewDestaques.frame = CGRectMake(0, -30, 320,30);
} completion:^(BOOL finished) {
}];
}];
It worked for me:
-(IBAction)btnAbrirDestaquesClick:(id)sender {
[self.viewDestaques setTranslatesAutoresizingMaskIntoConstraints:YES]; //respeita o frame que eu setar, independentemente das constraints
[self.viewDestaques setFrame:CGRectMake(self.viewDestaques.frame.origin.x, self.viewDestaques.frame.origin.y, self.viewDestaques.frame.size.width, 0)];
[self.viewDestaques setHidden:NO];
while (self.viewDestaques.frame.size.height < self.frameViewDestaquesOriginal.size.height) {
[UIView animateWithDuration:2.0 animations:^{
[self.viewDestaques setFrame:CGRectMake(self.viewDestaques.frame.origin.x, self.viewDestaques.frame.origin.y, self.viewDestaques.frame.size.width, self.view.frame.size.height + 10)];
}completion: ^(BOOL completed){
}];
}
}

View animation issue

I'm trying to flip animate a view into another view, which corresponds of a small section of the screen. Both views have the same dimensions and center.
I keep getting as a result the animation of the full screen. From the code below, can somebody please let me know what the heck i'm doing wrong?
Thank you,
-j
+ (void) flipView:(UIView*)viewA toView:(UIView*)viewB wait:(Boolean)wait
{
// get parent view
UIView *parent = [viewA superview];
CGRect r = viewA.frame;
// create container view with the same dimensions as ViewA
UIView *containerView = [[UIView alloc] initWithFrame:viewA.bounds];
containerView.center = viewA.center;
// attache both views to intermidiate view
[containerView addSubview:viewA];
[containerView addSubview:viewB];
[viewA removeFromSuperview];
[parent addSubview:containerView];
viewB.alpha = 0;
viewA.alpha = 1;
// Perform the annimation
__block BOOL done = NO;
[UIView transitionWithView:viewA
duration:2.0
options: (UIViewAnimationOptionTransitionFlipFromTop)
animations:^{
viewA.alpha = 0;
viewB.alpha = 1; }
completion:^(BOOL finished) {
done = YES;
// detach all views
[viewA removeFromSuperview];
[viewB removeFromSuperview];
[containerView removeFromSuperview];
}
];
if(wait) {
while (done == NO)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
}
First get rid of the old animation code (or at least commit it), it should look like this:
+ (void) flipView:(UIView*)viewA toView:(UIView*)viewB wait:(BOOL)wait
{
viewB.alpha = 0;
viewA.alpha = 1;
__block BOOL done = NO;
[UIView transitionWithView:viewA
duration:2.0
options: (UIViewAnimationOptionTransitionFlipFromTop)
animations:^{
viewA.alpha = 0;
viewB.alpha = 1; }
completion:^(BOOL finished) {
done = YES;
}
];
if(wait) {
while (!done)
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
}
Second, the superview of the subview that you want to transition is actually going to get the transition. So this means that you'll have to add viewA and viewB to a subview, add this subview to another view and then do the animation.
Here is a solution without any hacks with runloop. Just use the first message to run a transition to another view, and the second message to return to original state:
- (void) forwardTransitionFromView: (UIView *) viewA toView: (UIView *) viewB
{
NSAssert(viewA.superview, #"The 'from' view should be added to a view hierarchy before transition");
NSAssert(!viewB.superview, #"The 'to' view should NOT be added to a view hierarchy before transition");
CGSize viewASize = viewA.bounds.size;
viewB.frame = CGRectMake(0.0, 0.0, viewASize.width, viewASize.height);
[UIView transitionWithView:viewA
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[viewA addSubview:viewB];
}
completion:NULL];
}
- (void) backwardTransitionFromView: (UIView *) viewB toView: (UIView *) viewA
{
NSAssert([viewB.superview isEqual:viewA], #"The 'from' view should be a subview of 'to' view");
[UIView transitionWithView:viewA
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[viewB removeFromSuperview];
}
completion:NULL];
}
Why dont you flip both views in same direction?
-(void) FlipView:(bool) fromRight {
if(fromRight) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view1 cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:view2 cache:YES];
[UIView commitAnimations];
}
else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view1 cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view2 cache:YES];
[UIView commitAnimations];
}
}
and call the method like below:
[view1 setHidden:NO];
[self FlipView:YES];
[view2 setHidden:YES];
to reverse the animation:
[view2 setHidden:NO];
[self FlipView:NO];
[view1 setHidden:YES];

UiViewAnimation not call setAnimationDidStopSelector

I created this animation and I entered through the stop animation
[UIView setAnimationDidStopSelector:#selector (TermineAnimazioneGoPointAssignedWithDelay)] ;
When the animation part of the SetAnimationDidStop is not called ... Can you tell me why and where am I doing wrong ?
This is all the way animation :
-(void)setViewStatusGoPointassigned {
ViewgoPointAssignMessage = [[UIView alloc] initWithFrame:CGRectMake(115, 150, 100, 100) ];
ViewgoPointAssignMessage.backgroundColor = [UIColor colorWithRed:(77/255.0) green:(108/255.0) blue:(143/255.0) alpha:(1)];
[ViewgoPointAssignMessage.layer setCornerRadius:10.0f];
[ViewgoPointAssignMessage.layer setMasksToBounds:YES];
[UIView animateWithDuration:0.2 animations:^{
ViewgoPointAssignMessage.transform = CGAffineTransformMakeScale(1.05, 1.05);
ViewgoPointAssignMessage.alpha = 0.8; }
completion:^(BOOL finished){
[UIView animateWithDuration:2/15.0 animations:^{
ViewgoPointAssignMessage.transform = CGAffineTransformMakeScale(0.9, 0.9);
ViewgoPointAssignMessage.alpha = 0.9; }
completion:^(BOOL finished) {
[UIView animateWithDuration:1/7.5 animations:^{
ViewgoPointAssignMessage.transform = CGAffineTransformIdentity;
ViewgoPointAssignMessage.alpha = 1.0; } ];
} ];
} ];
[self.view addSubview:ViewgoPointAssignMessage];
[UIView setAnimationDidStopSelector:#selector(TermineAnimazioneGoPointAssignedWithDelay)];
}
-(void)TermineAnimazioneGoPointAssignedWithDelay {
[self performSelector:#selector(EliminazioneViewAfterDelay) withObject:self afterDelay:0.01];
}
-(void)EliminazioneViewAfterDelay {
CGRect endREct;
endREct = CGRectMake(350 , 0 , 330, 90);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.005];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(EliminaAnimazione)];
ViewgoPointAssignMessage.frame = endREct;
[UIView commitAnimations];
}
- (void)EliminaAnimazione {
[ViewgoPointAssignMessage removeFromSuperview];
}
First of all, to answer your question.
You may try
[UIView animateWithDuration:(NSTimeInterval)duration animations:^(void)animations completion:^(BOOL finished)completion]
and call [self TermineAnimazioneGoPointAssignedWithDelay] in the completion.
Secondly, [UIView setAnimationDidStopSelector:#selector(TermineAnimazioneGoPointAssignedWithDelay)] doesn't work because according to the reference, setAnimationDidStopSelector must be called between calls to the beginAnimations:context: and commitAnimations methods. Since your code doesn't comply to this rule, the function does not work either.
Hopefully it explains! Good Luck!

Hide both Navigation Controller and Tab Bar Controller with animation

I'm trying to hide a UITabBarController and UINavigationController simultaneously when a button is touch. I found a very nice code snippet here How to hide uitabbarcontroller but I have problem when trying to hide and animate both UINavigationController and the tabbarcontroller. I also found a lot of examples on the internet when they hide the tabbar using self.tabBarController.tabBar.hidden = YES but that only hides the button items not the black bar at the bottom.
After playing a lot around I can get to make both animate correctly because I think that it's related to the hiding of the Navigation Controller which makes the size of the whole window to change on the fly.
-(IBAction)touchImage:(id)sender {
if (isImageFullScreen) {
isImageFullScreen = NO;
[self.navigationController setNavigationBarHidden:NO animated:YES];
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionCurveLinear
animations:^
{
hotelImageButton.frame = CGRectMake(0,20,320,92);
[self showTabBar:self.tabBarController];
}
completion:^(BOOL finished)
{
}];
} else {
isImageFullScreen = YES;
[self.navigationController setNavigationBarHidden:YES animated:YES];
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionCurveLinear
animations:^
{
hotelImageButton.frame = CGRectMake(0,0,320,480);
[self hideTabBar:self.tabBarController];
}
completion:^(BOOL finished)
{
}];
}
}
The hideTabBar and showTabBar methods are the ones from the other post I linked above.
I also tried some other combinations but I can't make it look good. Any ideas?
Thanks in advance.
I tried that code now and I see that the UITabBar show animation doesn't take place smoothly.
I managed to make it smoother by adjusting the duration period for the tabbar showing animation to be lower.
[UIView setAnimationDuration:0.2];
Hopefully that works.
EDIT:
Please try this code, it resizes the parent view to be bigger in 1 animation transaction in such a way that only the bars are hidden and the content is shown.
- (IBAction)TestButton1:(UIButton *)sender {
if(!isAnimating){
if(isTabBarAndNavBarHidden){
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^
{
isAnimating=YES;
CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;
[self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, 0, self.tabBarController.view.frame.size.width, screen_height)];
[self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, statusBar_height, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
}
completion:^(BOOL finished)
{
isTabBarAndNavBarHidden=NO;
isAnimating=NO;
}];
}else{
[UIView transitionWithView:self.view
duration:0.5
options:UIViewAnimationOptionTransitionNone
animations:^
{
isAnimating=YES;
CGFloat statusBar_height=[[UIApplication sharedApplication] statusBarFrame].size.height;
CGFloat screen_height=[UIScreen mainScreen].bounds.size.height;
[self.tabBarController.view setFrame:CGRectMake(self.tabBarController.view.frame.origin.x, statusBar_height-self.navigationController.navigationBar.frame.size.height, self.tabBarController.view.frame.size.width, screen_height+self.navigationController.navigationBar.frame.size.height+self.tabBarController.tabBar.frame.size.height-statusBar_height)];
[self.navigationController.navigationBar setFrame:CGRectMake(self.navigationController.navigationBar.frame.origin.x, 0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
}
completion:^(BOOL finished)
{
isTabBarAndNavBarHidden=YES;
isAnimating=NO;
}];
}
}
}
This code is for iPhone 4/4S.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (self.lastContentOffset > scrollView.contentOffset.y)
{
NSLog(#"Scrolling up");
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
[self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
} completion:
^(BOOL finished) {
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
} completion:^(BOOL finished) {
//
}];
}];
}
else if (self.lastContentOffset < scrollView.contentOffset.y)
{
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.navigationController.navigationBar setFrame:CGRectMake(0, -60, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
[self.tabBarController.tabBar setFrame:CGRectMake(0, 480, 320, 50)];
} completion:
^(BOOL finished) {
[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
} completion:^(BOOL finished) {
}];
}];
NSLog(#"Scrolling Down");
}
self.lastContentOffset = scrollView.contentOffset.y;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tabBarController.tabBar setFrame:CGRectMake(0, 430, 320, 50)];
[self.navigationController.navigationBar setFrame:CGRectMake(0, 20, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
// Do any additional setup after loading the view.
}

Resources