How to make an animation of "cancel" button when touch the UITextField - ios

I am now creating an App that has a browser like Safari:
There is a UITextField(left side) and UISearchBar(right side) inserted into the UINavigationBar on the top of the view.
I am wondering if there is a way to add an animation on a "Cancel" button at the time it appears when user touches the UITextField.
The animation is just like it is on the Safari: when User touches the UITextField, the "Cancel" button comes from right outside of the window while the UISearchBar is fading.
Note that I am initializing the "Cancel" button by UIBarbuttonItem(UIBarButtonSystemItemCancel).
Any help would be appreciated.
Update:
Finally I found the anwser by myself. It seems that you cannot assign a specific position where UIBarButtonItem shows up or hide. So one possible way to solve this problem is to customize a UIButton just looks like the UIBarButtonItem, here is the code:
-(void)viewDidLoad {
......
CGSize backgroundStretchPoints = {4, 2};
UIImage *image = [[UIImage imageNamed:#"UINavigationBarDefaultButton.png"] stretchableImageWithLeftCapWidth:backgroundStretchPoints.width
topCapHeight:backgroundStretchPoints.height];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(330, 23, 80, 30); // initialize the button outside the window
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:#"Cancel" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
[button addTarget:self action:#selector(cancelButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[navigationBar addSubview: button];
.......
}
and when you click the UITextField(i.e.), the cancel button will slide into the window:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView animateWithDuration:0.25 animations:^{
button.frame = CGRectMake(232, 23, 80, 30); // here is the position button will slide into window with animation
}];
}

Related

show and remove same button in iOS

I have one UIButton. Now I add button on Subview :-
UIButton* button4 = [[UIButton alloc] initWithFrame:frame4];
[button4 setBackgroundImage:image3 forState:UIControlStateNormal];
[button4 setShowsTouchWhenHighlighted:YES];
[button4 addTarget:self action:#selector(Nofication:) forControlEvents:UIControlEventTouchDown];
- (void)Nofication:(id)sender
{
share=[[UIView alloc]init];
share.frame=CGRectMake(300, 60, 130, 100);
[self.view addSubview:share];
[UIView animateWithDuration:2.0
animations:^{
share.frame =CGRectMake(180, 50, 130, 100); // its final location
}];
share.backgroundColor=[UIColor yellowColor];
login=[[UIButton alloc]init];
login.frame=CGRectMake(0, 0, 130, 50);
[login setBackgroundImage:[UIImage imageNamed:#"login.png"] forState:UIControlStateNormal];
[login addTarget:self action:#selector(asubmit:) forControlEvents:UIControlEventTouchDown];
[share addSubview:login];
login.layer.borderColor = [[UIColor whiteColor]CGColor];
policies=[[UIButton alloc]init];
policies.frame=CGRectMake(0, 50, 130, 50);
[policies setBackgroundImage:[UIImage imageNamed:#"Policies.png"] forState:UIControlStateNormal];
[policies addTarget:self action:#selector(apolicies:) forControlEvents:UIControlEventTouchDown];
[share addSubview:policies];
}
When I click button it's open subview working nice. But now I need if subview is showing in that time click button it's need to hidden subview. If subview is not showing in that time when I click button it's need to show subview. So Please give me any idea about it.
How to Show and Hidden same UIButton
thanks in Advance .
There is a hidden property on UIView. https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instp/UIView/hidden
if (button.isHidden) {
button.hidden = NO;
}
Is it what asked?
Where subview is the view (or UIButton) you are trying to hide if shown, and show if hidden.
[subview setHidden:![subview isHidden]];
Edit :
In your Notification function :
// Be sure that the view is loaded, maybe load it at a different place or use a boolean
if([subview isHidden]){
[subview setHidden:false];
}else{
[subview setHidden:true];
}
You can set button4.selected = !button4.selected in Nofication: method.
Then you can check button4.isSelected and hide/show subview with hidden property
Hope it helps.

Dim UIButton text when tapped

I am using custom UIButtons, and what I want to do is make it so that when a user touches the button it dims, or turns a grayish color, like the regular button does.
I just want the text to temporarily change color temporally when the users lifts there figure it will be back to the regular color.
I have tried this code:
button.showsTouchWhenHighlighted = TRUE;
but that just makes a white circle around it which is not what I'm looking for.
Thanks for the help.
The easiest way I've come across is:
[myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
Try changing alpha of title label on button click like this
- (IBAction)buttonClick:(id)sender {
UIButton *button = sender;
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut animations:^
{
[button.titleLabel setAlpha:0.5];
} completion:^(BOOL finished)
{
[button.titleLabel setAlpha:1];
}];
}
You can utilize the following UIControlEvents:
UIControlEventTouchDown
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
Assign target-action methods to them appropriately like so:
//when touch initiated
[myButton addTarget:self
action:#selector(buttonTouchStartAct:)
forControlEvents:UIControlEventTouchDown];
//on touch released outside button bounds
[myButton addTarget:self
action:#selector(buttonTouchEndAct:)
forControlEvents:UIControlEventTouchUpOutside];
//on touch released while still inside button bounds (most commonly used)
[myButton addTarget:self
action:#selector(buttonAct:)
forControlEvents:UIControlEventTouchUpInside];
-(void)buttonTouchStartAct:(UIButton *)sender
{
[sender.titleLabel setTextColor:[UIColor redColor]];
}
-(void)buttonTouchEndAct:(UIButton *)sender
{
[sender.titleLabel setTextColor:[UIColor greenColor]];
}
-(void)buttonAct:(UIButton *)sender
{
//touch ended in this case too
[self buttonTouchEndAct:sender];
//...
//your main button logic
//...
}
//when touch initiated
[myButton addTarget:self
action:#selector(buttonTouchStartAct:)
forControlEvents:UIControlEventTouchDown];
//on touch released outside button bounds
[myButton addTarget:self
action:#selector(buttonTouchEndAct:)
forControlEvents:UIControlEventTouchUpOutside];
//on touch released while still inside button bounds (most commonly used)
[myButton addTarget:self
action:#selector(buttonAct:)
forControlEvents:UIControlEventTouchUpInside];
and then inside the method change the color of the button

Programmatic Custom UIButton Does Not Appear After Timer

I have been trying to write a program in which a button appears after a timer fires. The timer is working properly, as is the firing event (I have tested this with different events). However, the button I am making appear is not showing up. Also, I would like the button background image to change when the button is pressed and held. I have tried making the button just have a background color instead of an image, but that does not work either. The code is located in the Timer selector within the view controller Any help is appreciated. Thanks!
- (void)timerFireMethod:(NSTimer *)timer
{
Button = [UIButton buttonWithType:UIButtonTypeCustom];
Button.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);
[Button setBackgroundImage:[UIImage imageNamed:#"buttonUnheldBGImage"]
forState:UIControlStateNormal];
[Button setBackgroundImage:[UIImage imageNamed:#"buttonHeldBGImage"]
forState:UIControlStateHighlighted];
[Button setTitle:#"Press and Hold" forState:UIControlStateNormal];
[Button addTarget:self action:#selector(aSelector:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:Button];
}
The timer class that calls this is:
- (void)timer
{
globalTimer = [NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:#selector(timerFireMethod:)
userInfo:Nil
repeats:NO];
}
globalTimer is a global NSTimer
You have set the center of the button but you also need the frame of it, i believe that the button is showing now but his frame is (0,0).
You have to set a size to your button as well. If you want it at the center of your super view just do :
CGRect buttonFrame = CGRectMake(self.view.frame.size.width/2-100,self.view.frame.size.height/2-100, 200,200);
Button.frame = buttonFrame;
The center property is essentialy used for UI Element that already have a size.
Just replace your first 2 lines,
UIButton * Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Button.frame = CGRectMake(20, 200, 200, 200);

Adding a close button to the modal view that is partially off the view

I am trying to add a "X" button to the top of the modal view so if the user presses that it closes the modal view. I looked at the solution proposed here how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?
But I have two follow up questions here as i am fairly new to ios programming
The solution proposed in the above page does not seem to work for me. I tried it with a normal button first in the viewDidLoad of the modal VC and I am seeing it appear only within the bounds of modal view and not outside as I want.
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
buttonView.backgroundColor = [UIColor brownColor];
CGRect buttonFrame = CGRectMake( 0, 0, 100, 50 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: #"Close" forState: UIControlStateNormal];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[button addTarget:self
action:#selector(closeButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
CGRect parentView = self.view.superview.frame;
CGRect currentView = self.view.frame;
CGRect buttonViewFrame = buttonView.frame;
buttonViewFrame.origin = CGPointMake(parentView.origin.x - buttonViewFrame.size.width/2.0f, parentView.origin.y - buttonViewFrame.size.height/2.0f);
[buttonView setFrame:buttonViewFrame];
[self.view addSubview:buttonView];
what I am seeing is
How do I draw the "X" button do I use CGRect to draw it our or instead of adding a button in the above example should I just add a image with the "X" in it as the subview?
thanks in advance for the help
do like this, hope helps u :)
//adding button to your view
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 300, 350)];
aView.backgroundColor = [UIColor blueColor];
aView.tag = 100;
[self.view addSubview:aView];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitle:#"CLOSE" forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
aButton.frame = CGRectMake(aView.bounds.origin.x, aView.bounds.origin.y, 80, 30);// adjust with respect to bounds
aButton.backgroundColor = [UIColor redColor];
[aView addSubview:aButton];
[aView release]; //i am doing without ARC
The other option is to create a larger view and put your UITableView within this, along with your close button. The close button can then be at position (0,0) and the UITableView can start at (25,25).
This prevents complications - I have seen clipped views fail to recognise the touch outside of the main view, so your 50x50 button only has a touch area of 25x25.

UIButton in IOS responds to first touch and then does not respond to any subsequent touch events

I have created a UIButton programmatically that correctly changes background color as desired when button is touched down for the first time. After that, the button does not respond to any subsequent touch events.
I have instrumented the color change method with NSLog and this method is not called after the first touch, even though the button visually appears to respond to subsequent touches on the iPhone screen. It seems the subsequent touches are not actually triggering any event (eg. TouchDown), but I can't figure out why not.
I have searched every post on sof related to this issue, but none of the answers seem to solve my problem.
The button is declared in the header file as:
UIButton *playerOneButton;
UIButton *playerTwoButton;
Here is the button initialization code that I use after the UIViewController loads:
- (void)viewDidLoad
{
[super viewDidLoad];
//Create initial view of Button One before any selection is made
playerOneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playerOneButton.frame = CGRectMake(5.0f, 20.0f, 149.0f, 31.0f);
[playerOneButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15.0]];
[playerOneButton setTitle:playerOneName forState:UIControlStateNormal];
[self.view addSubview:playerOneButton];
//Create initial view of Button Two before any selection is made
playerTwoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
playerTwoButton.frame = CGRectMake(166.0f, 20.0f, 149.0f, 31.0f);
[playerTwoButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15.0]];
[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal];
[self.view addSubview:playerTwoButton];
// Add correct names to both buttons
[playerOneButton setTitle:playerOneName forState:UIControlStateNormal];
[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal];
// Link buttons to methods that will toggle colors between Green for selected button and Red for unselected button
[playerOneButton addTarget:self action:#selector(setPlayerOneButtonStatus) forControlEvents:UIControlEventTouchDown];
[playerTwoButton addTarget:self action:#selector(setPlayerTwoButtonStatus)
forControlEvents:UIControlEventTouchDown];
}
and the method that is invoked to actually change the colors:
-(void) setPlayerOneButtonStatus;
{
playerOneButton.selected = YES;
// Load colored images
UIImage *imageGreen = [UIImage imageNamed:#"button_green.png"];
UIImage *imageRed = [UIImage imageNamed:#"button_red.png"];
// And create the stretchy versions.
float wGreen = imageGreen.size.width / 2, hGreen = imageGreen.size.height / 2;
UIImage *stretchGreen = [imageGreen stretchableImageWithLeftCapWidth:wGreen topCapHeight:hGreen];
float wRed = imageRed.size.width / 2, hRed = imageRed.size.height / 2;
UIImage *stretchRed = [imageRed stretchableImageWithLeftCapWidth:wRed topCapHeight:hRed];
// Now create button One with Green background color
playerOneButton = [UIButton buttonWithType:UIButtonTypeCustom];
playerOneButton.frame = CGRectMake(5.0f, 20.0f, 149.0f, 31.0f);
[playerOneButton setBackgroundImage:stretchGreen forState:UIControlStateNormal];
[playerOneButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15.0]];
[playerOneButton setTitle:playerOneName forState:UIControlStateNormal];
[self.view addSubview:playerOneButton];
// Now create button Two with Red background color
playerTwoButton = [UIButton buttonWithType:UIButtonTypeCustom];
playerTwoButton.frame = CGRectMake(166.0f, 20.0f, 149.0f, 31.0f);
[playerTwoButton setBackgroundImage:stretchRed forState:UIControlStateNormal];
[playerTwoButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:15.0]];
[playerTwoButton setTitle:playerTwoName forState:UIControlStateNormal];
[self.view addSubview:playerTwoButton];
}
You are creating a new playerOneButton in the action method without assigning a target/action to it.

Resources