iOS/Xcode - Toggle button changing position - iphone

I want a button that toggles the position of an element on click.
My current code:
-(IBAction)menuTouched{
// Push menu
UITableView * sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)];
sideMenu.backgroundColor = [UIColor redColor];
[self.view addSubview:sideMenu];
// Animate
[UIView animateWithDuration:1.25 animations:^{
sideMenu.frame = CGRectMake(960, 88, 63, 661);
}];
}
When clicking the button it animates the tableview in just like I want to. But now I want to animate the tableview out by pressing the exact same button. So in short I want the button to toggle in and out a tableview by changing the X position. What is the best/easiest approach?
Thanks in advance.

inside your .h file add
UITableView * sideMenu;
BOOL checkInOut;
inside .m file add
- (void)viewDidLoad
{
[super viewDidLoad];
checkInOut=False;
// Push menu
sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)];
sideMenu.backgroundColor = [UIColor redColor];
[self.view addSubview:sideMenu];
}
-(IBAction)menuTouched
{
checkInOut=!checkInOut
if(checkInOut)
{
// Animate
[UIView animateWithDuration:1.25 animations:^{
sideMenu.frame = CGRectMake(960, 88, 63, 661);
}];
}
else
{
[UIView animateWithDuration:1.25 animations:^{
sideMenu.frame = CGRectMake(1060, 88, 63, 661);
}];
}
}

You will need a bool to store the state where you are, but you can just use the button.selected state for that.. Something like that:
- (IBAction)menuTouched:(id)sender
{
sender.selected = !sender.selected;
if (sender.selected)
{
// toggle on stuff
}
else
{
// toggle off stuff
}
}

Try it....
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.5];
myButton.frame = CGRectMake(0,420,320,120);
[UIView commitAnimations];
Hope i helped.

you can perform the same animation with different frame as you set previously
sideMenu.frame = CGRectMake(1060, 88, 63, 661);
with the check to see have you togged the table in or out to perform respective action.
Hope this helps.

You have to receive as UIButton reference instead of id
- (IBAction)menuTouched:(UIButton *)sender
{
sender.selected = !sender.selected;
if (sender.selected)
{
[UIView animateWithDuration:1.25 animations:^
{
UITableView * sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)];
sideMenu.backgroundColor = [UIColor redColor];
[self.view addSubview:sideMenu];
sideMenu.frame = CGRectMake(960, 88, 63, 661);
}];
}
else
{
[UIView animateWithDuration:1.25 animations:^{
[sideMenu removeForSuperView];
}];
}
}

1.set a flag bit to represent UITableView's current state.The statement need 3 variables.
BOOL isShow;
UITableView *sideMenu;
CGFloat posX;
2.initial setting in viewDidLoad
-(void)viewDidLoad {
isShow=NO;
//to do something...
sideMenu = [[UITableView alloc] initWithFrame:CGRectMake(1060, 88, 63, 661)];
sideMenu.backgroundColor = [UIColor redColor];
[self.view addSubview:sideMenu];
}
3.What UITableView shows is just the change of X-coordinate of frame.so you can calculate posX depend on toggles 'state.and notice don't create UITableView at each time clicking.put the UITableView creat code on viewDidLoad function.
-(IBAction)menuTouched{
posX=isShow ? 1060 : 960
// Animate
[UIView animateWithDuration:1.25 animations:^{
sideMenu.frame = CGRectMake(posX, 88, 63, 661);
}];
isShow=!isShow; //boggle button
}

Related

Drawing Line in Two Views At a Time

I am doing an app in which i need to draw lines in one view and that automatically should appear in the other view. Any help would be appreciated..
I have tried this..
- (void)viewDidLoad
{
[super viewDidLoad];
slv = [[SmoothLineView alloc] initWithFrame:CGRectMake(DrawingView.bounds.origin.x, DrawingView.bounds.origin.y + 42, DrawingView.bounds.size.width, DrawingView.bounds.size.height - 42)];
slv.delegate = self;
[DrawingView addSubview:slv];
}
-(IBAction)btnAnotherView:(UIButton *)sender
{
[zoomingTypingView setUserInteractionEnabled:YES];
if(sender.tag == 123)
{
[self.view setBackgroundColor:[UIColor colorWithWhite:0.500 alpha:1.000]];
[UIView animateWithDuration:0.5
delay:0.1
options:UIViewAnimationTransitionCurlUp
animations:^{
//animation code
zoomingTypingView.frame = CGRectMake(0, 549, 768, 451);
} completion:^(BOOL finished) {
}];
CGRect imageFrame = CGRectMake(50, 50, 220, 120);
ResizableView = [[SPUserResizableView alloc] initWithFrame:imageFrame];
CGRect gripFrame = CGRectMake(50,50, 220,120);
UIView *zoom = [[UIView alloc]initWithFrame:gripFrame];
UIScrollView *zoomscroll = [[UIScrollView alloc]initWithFrame:gripFrame];
[zoomscroll setZoomScale:32.0];
[zoomscroll setMaximumZoomScale:32.0];
[zoom addSubview:zoomscroll];
[ResizableView setContentView:zoom];
ResizableView.delegate = self;
[DrawingView addSubview:ResizableView];
sender.tag = 246;
}
else
{
[UIView animateWithDuration:0.5
delay:0.1
options:UIViewAnimationTransitionCurlUp
animations:^{
//animation code
zoomingTypingView.frame = CGRectMake(0, 999, 768, 451);
} completion:^(BOOL finished) {
[self.view setBackgroundColor:[UIColor whiteColor]];
}];
[ResizableView removeFromSuperview];
[DrawingView addSubview:slv1];
sender.tag = 123;
}
}
In the above Image WhiteColored View is the new View and if i draw in that whiteColoredView that should reflect in both the views.
You can get more idea by looking at the NOTE TAKER HD APP.
http://www.youtube.com/watch?v=FdGDnUKZcMM
If you have two views defined:
#property(nonatomic) UIView *view1;
#property(nonatomic) UIView *view2;
instead of do self.view, you can have function does the drawing for one view:
-(void)drawView:(UIView*)view{
//your drawing code here
}
And just call the same function twice with passing different views.

UIView in UIAlert

I want to show UIView in UIAlert and alert should be display in all another view and also when application running in background. Is it Possible ? if No then how can i do it?
Try This Code :-
In view didload Method :
-(void)viewDidLoad
{
flag=YES;
[super viewDidLoad];
}
In viewWillAppear Method :
-(void)viewWillAppear:(BOOL)animated
{
v=[[UIView alloc]initWithFrame:CGRectMake(0, 490, 320, 300)];
v1.layer.cornerRadius = 10.0;
v1.clipsToBounds = YES;
v1.frame=CGRectMake(0, 490, 320, 108);
}
And
-(IBAction)btn_Clicked
{
if (flag == NO)
{
v=[[UIView alloc]initWithFrame:CGRectMake(0, 490, 320, 300)];
v1.layer.cornerRadius = 10.0;
v1.clipsToBounds = YES;
v1.frame=CGRectMake(0, 490, 320, 108);
v.hidden=NO;
v.frame=CGRectMake(0, 0, 320, 460);
v.backgroundColor=[UIColor colorWithWhite:0 alpha:0.2];
[self.view addSubview:v];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
v1.frame=CGRectMake(69, 176, 185, 108);
[v addSubview:v1];
[UIView commitAnimations];
}
else
{
v.hidden=NO;
v.frame=CGRectMake(0, 0, 320, 460);
v.backgroundColor=[UIColor colorWithWhite:0 alpha:0.2];
[self.view addSubview:v];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
//v1.backgroundColor=[UIColor colorWithWhite:0 alpha:0.3];
v1.frame=CGRectMake(69, 176, 185, 108);
[v addSubview:v1];
[UIView commitAnimations];
}
}
And
-(IBAction)cancel_Clicked
{
flag=NO;
v.hidden=YES;
}
When your app goes in background you dont have that much access in showing UIAlertView, you can try UILocalNotification.
But I guess you want a custom popup which is your own view.
just try creating a UIView and set properties from Interface Builder. and add as a subview.
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 290, 290)];
[self.view addSubView:v];
but once your app is in background, you can't do anything anymore.
You can add a UIView in UIAlertView,
but application running in background you can't, it 's only the notification style.

Flip SubView on UIScrollView not working

I'm coding an iPhone TabBar Project with UiNavigation, the first view contain UIScrollView and the scrollview contain views every subview contain imageview,anUiImage and a UiButton follows
is the function that build the first screen
//*******************************************************
//* Build the main screen *
//*******************************************************
-(void) buildScreen{
sing.viewsArray = [[NSMutableArray alloc]init];
int Vve = 10;
int Vho = 10;
int wi = 95;
int hi = 95;
int ArrayIndex = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
staticView = [[UIView alloc] initWithFrame:CGRectMake(Vve, Vho, 100, 100)];
[staticView setTag:ArrayIndex];
staticImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[sing.stdPicArray objectAtIndex:ArrayIndex]]];
[staticImage setFrame:CGRectMake(0, 0, wi, hi)];
[staticView addSubview:staticImage];
viewButton = [UIButton buttonWithType:UIButtonTypeCustom];
viewButton.frame = CGRectMake(0, 0, wi, hi);
[viewButton addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[viewButton setTag:ArrayIndex];
[sing.buttonsArray addObject:viewButton];
[staticView addSubview:viewButton];
[sing.viewsArray addObject:staticView];
[MainScroll addSubview:[sing.viewsArray objectAtIndex:ArrayIndex]];
Vve += 100;
ArrayIndex++;
}
Vve = 10;
Vho += 100;
}
}
When I click on the UiButton I want to add another UIView to the UIScrollView and make a "UIViewAnimationTransitionFlipFromLeft" when the view is added to the scroll view
tried the following code
-(void)animateFlip{
[UIView transitionWithView:flipViewBack
duration:1.0
options:UIViewAnimationTransitionFlipFromLeft
animations:^{ [self.view addSubview:flipViewBack]; }
completion:^(BOOL f){ }];
}
didn't work just added the flipViewBack
-(void)animateFlip{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.flipViewFront
cache:YES];
[MainScroll addSubview:flipViewFront];
[UIView setAnimationDidStopSelector:#selector(flipAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
didn't work just like the other one, the next code did flip the main view with the view I wanted, but it didn't flip only the the view I want
-(void)animateFlip{
[UIView beginAnimations:nil
context:nil];
[UIView setAnimationDuration:1.0];
[UIView transitionFromView:self.view
toView:flipViewBack
duration:2
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:NULL];
[UIView setAnimationDidStopSelector:#selector(flipAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
//set transformation
[UIView commitAnimations];
}
Does any body know how to do it' I looked everywhere and all the code I found didn't work for me
Thanks
I tried another option suggested in this link
iPhone subview flip between 2 views and it didn't work for me
this is the function that I use on click in the last 3 lines im adding to the flipView (container) the flipViewFront view then I add the flipView to the mainScroll view and then I go to execute the animation in the animateFlip that I copied from your sample
//*******************************************************
//* Click on the picture as button *
//*******************************************************
-(void) buttonClick:(id) sender{
UIButton *button = (UIButton *)sender;
int row = [button superview].tag;
NSLog(#"Button pressed tag: %i",row);
self.flipView = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipView.backgroundColor = [UIColor clearColor];
self.flipViewBack = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipViewFront = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipViewFront.backgroundColor = [UIColor whiteColor];
self.flipViewImage = [UIImage imageNamed:[sing.stdPicArray objectAtIndex:row]];
self.filpImage = [[UIImageView alloc]initWithImage:self.flipViewImage];
[self.flipViewBack addSubview:self.filpImage];
self.flipCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.flipCloseButton setFrame:CGRectMake(0, 0, 24, 24)];
[self.flipCloseButton setImage:[UIImage imageNamed:#"close_button.png"] forState:UIControlStateNormal];
[self.flipCloseButton addTarget:self action:#selector(closwFlip:) forControlEvents:UIControlEventTouchUpInside];
[self.flipViewBack addSubview:flipCloseButton];
self.flipOkButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 0, 190, 190)];
[self.flipCloseButton addTarget:self action:#selector(continueTo:) forControlEvents:UIControlEventTouchUpInside];
[self.flipViewBack addSubview:flipOkButton];
[flipView addSubview:flipViewFront];
[MainScroll addSubview:flipView];
[self animateFlip];
}
found the problem, but not the answer, if it works directly from the ViewDidLoad it and the first view is on screen all is well and the flip works' if I change it so that the want to add a view and flip it in one event it shows me the second(flipped) view without the flip
Your problems seems to be that you are trying to animate the flip on the view before it is added to the view hierarchy.
Instead you should add the back view to a common container view (that later will contain the front view) and then flip the container view while adding the front and removing the back, like this:
// Container view is a view that is already added to your view hierarchy.
// It contains the back view. The front view will be added to it.
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview]; // This is already added to the container
[containerView addSubview:frontView]; // Not yet added ...
}
completion:NULL];
Update
This is all the code I wrote when answering this question. It works for me. If it still doesn't animate for you then you probably haven\t set up your view hierarchy the same way.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
containerView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
[self.view addSubview:containerView];
frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[frontView setBackgroundColor:[UIColor orangeColor]];
backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[backView setBackgroundColor:[UIColor redColor]];
[containerView addSubview:backView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(flip)];
[self.view addGestureRecognizer:tap];
}
- (void)flip {
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview];
[containerView addSubview:frontView];
}
completion:NULL];
}

searchDisplayControllerWillEndSearch not resizing searchbar

Just wondering why and need to solve it. i have this searchbar looks like this.
which clicked will move up to the top and expend to 0,0,320,searchbar's height.
which clicked Cancel, it should resize back to 33,55,270, searchbar's height. but it doesn't
the searchDisplayControllerDidEndSearch will work to resize back, however there is a 1 sec lag that user could see the changes. any body why in searchDisplayControllerWillEndSearch the animation resize wound't work ?.
thanks for the comments and feedback.
- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller
{
[UIView animateWithDuration:.1
animations:^{
controller.searchResultsTableView.frame = frogTableView.frame;
searchDisplayController.searchResultsTableView.frame = frogTableView.frame;
searchDisplayController.searchBar.frame = CGRectMake(32,
55,
270,
searchDisplayController.searchBar.frame.size.height);
searchBar.frame = CGRectMake(32,
55,
270,
searchBar.frame.size.height);
frogTableView.frame = CGRectMake(frogTableView.frame.origin.x,
121,
frogTableView.frame.size.width,
frogTableView.frame.size.height);
notePad.frame = CGRectMake(notePad.frame.origin.x,
45,
notePad.frame.size.width,
notePad.frame.size.height);
searchDisplayController.searchResultsTableView.frame = frogTableView.frame;
}
completion:^(BOOL finished){
//whatever else you may need to do
}];
}
I had the same trouble after I started to use UISearchBar with UISearchDisplayController. The solution I came up with is to place a search bar on a view:
UIView * searchBarSubview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MENU_WORK_AREA_WIDTH, SEARCH_BAR_HEIGHT)] ;
searchBarSubview.autoresizesSubviews = YES;
searchBarSubview.backgroundColor = [UIColor clearColor];
UISearchBar * globalSearchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, MENU_WORK_AREA_WIDTH, SEARCH_BAR_HEIGHT)] autorelease];
globalSearchBar.tintColor = [UIColor blackColor];
globalSearchBar.delegate = self;
[globalSearchBar setBackgroundImage:[UIImage alloc]];
and resize the view instead of search bar in searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
// Slide in to the final destination
searchBarSubview.frame = CGRectMake(0, 0, MENU_WORK_AREA_WIDTH, SEARCH_BAR_HEIGHT);
[UIView commitAnimations];

How to animate expand and shrink uiview in place?

I have a series of uiviews, and i want to create an animation where a button press event will expand the uiview from the bottom and reveal more information about the view. Another button press will shrink the view back to original.
How do i proceed with this?
There are a series of uiviews laid out in a horizontal scroll view like a coverflow. I want to display additional information about each uiview on click of a button.
Do drop a comment if you need more information.
TIA,
Praveen S
This code will expand your view...do the reverse to shrink it
CGRect tempFrame=view.frame;
tempFrame.size.width=200;//change acco. how much you want to expand
tempFrame.size.height=200;
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:0.2];
view.frame=tempFrame;
[UIView commitAnimations];
For that use this code
In .h file
#import <UIKit/UIKit.h>
#interface ExpandedViewController : UIViewController
{
UIView * expandiView;
BOOL viewExpanded;
}
- (IBAction)expandOrShrinkView:(id)sender;
#end
and in your .m file
- (void)viewDidLoad
{
[super viewDidLoad];
aview = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
aview.backgroundColor = [UIColor redColor];
[self.view addSubview:aview];
viewExpanded = NO;
}
And in your button action add this code
- (IBAction)expandOrShrinkView:(id)sender {
[UIView animateWithDuration:0.8f delay:0.0f options:UIViewAnimationOptionTransitionNone animations: ^{
if (!viewExpanded) {
viewExpanded = YES;
aview.frame = CGRectMake(10, 10, 100, 200);
}else{
viewExpanded = NO;
aview.frame = CGRectMake(10, 10, 100, 100);
}
} completion:nil];
}
When you click the button first time it will expand the view from bottom and when you click the button second time it will shrink the View from bottom change the frame size to your need..
And if you want shrink function in another button than use two action method like this
- (IBAction)expandView:(id)sender {
[UIView animateWithDuration:0.8f delay:0.0f options:UIViewAnimationOptionTransitionNone animations: ^{
aview.frame = CGRectMake(10, 10, 100, 200);
} completion:nil];
}
- (IBAction)ShrinkView:(id)sender {
[UIView animateWithDuration:0.8f delay:0.0f options:UIViewAnimationOptionTransitionNone animations: ^{
aview.frame = CGRectMake(10, 10, 100, 100);
} completion:nil];
}