I have created a UIButton; actually, a few buttons, and I have initialized them and set them up in the
-(void)viewDidLoad
method. My issue now, is connecting them to the method that I have created. I have created a typedef enum to replace the numbers for the tags of my buttons with names to certain situations. I do not know if this is the issue or what, but I am confused on why when I press the button(s), they are not working. My code is below.
My enum:
typedef enum {
DiseasesStrepThroat = 1,
DiseasesFlu = 2,
DiseasesChickenPox = 3,
DiseasesSmallPox = 4,
DiseasesMeasels = 5,
DiseasesCommonCold = 6
} DiseasesTagNumber;
I have initialized all of the UIButton objects; as well as my IBAction in my .h file already, so I don't have to worry about that part.
Here is my view did load
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 700, 768, 125)];
scrollView.contentSize = CGSizeMake(1000, 125);
scrollView.backgroundColor = [UIColor darkGrayColor];
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.autoresizesSubviews = NO;
[self.view addSubview:scrollView];
//////////////////////////////////////////
// //
// creating all the buttons //
// //
//////////////////////////////////////////
// strep throat
strep = [UIButton buttonWithType:UIButtonTypeRoundedRect];
strep.frame = CGRectMake(10, 10, 110, 100);
[strep setTitle:#"Strep" forState:UIControlStateNormal];
[strep setTag:DiseasesStrepThroat];
[scrollView addSubview:strep];
// the flu
flu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
flu.frame = CGRectMake(130, 10, 110, 100);
[flu setTitle:#"Flu" forState:UIControlStateNormal];
[flu setTag:DiseasesFlu];
[scrollView addSubview:flu];
// chicken pox
chickenPox = [UIButton buttonWithType:UIButtonTypeRoundedRect];
chickenPox.frame = CGRectMake(250, 10, 110, 100);
[chickenPox setTitle:#"Chicken Pox" forState:UIControlStateNormal];
[chickenPox setTag:DiseasesChickenPox];
[scrollView addSubview:chickenPox];
// small pox
smallPox = [UIButton buttonWithType:UIButtonTypeRoundedRect];
smallPox.frame = CGRectMake(370, 10, 110, 100);
[smallPox setTitle:#"Small Pox" forState:UIControlStateNormal];
[smallPox setTag:DiseasesSmallPox];
[scrollView addSubview:smallPox];
// measels
measels = [UIButton buttonWithType:UIButtonTypeRoundedRect];
measels.frame = CGRectMake(490, 10, 110, 100);
[measels setTitle:#"Measels" forState:UIControlStateNormal];
[measels setTag:DiseasesMeasels];
[scrollView addSubview:measels];
// common cold
commonCold = [UIButton buttonWithType:UIButtonTypeRoundedRect];
commonCold.frame = CGRectMake(610, 10, 110, 100);
[commonCold setTitle:#"Common Cold" forState:UIControlStateNormal];
[commonCold setTag:DiseasesCommonCold];
[scrollView addSubview:commonCold];
NSArray *array = [[NSArray alloc] initWithObjects:strep, flu, chickenPox, smallPox, measels, commonCold, nil];
for(UIButton *b in array) {
b.showsTouchWhenHighlighted = YES;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
// //
// CREATING ALL THE TARGETS //
// //
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
[strep addTarget:self action:#selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
[flu addTarget:self action:#selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
[chickenPox addTarget:self action:#selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
[smallPox addTarget:self action:#selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
[measels addTarget:self action:#selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
[commonCold addTarget:self action:#selector(showDiseasesWithTag:) forControlEvents:UIControlEventTouchUpInside];
}
and now my IBAction method
- (IBAction) showDiseasesWithTag:(NSInteger)senderTag {
switch (senderTag) {
case DiseasesStrepThroat:
a = [[UIAlertView alloc]
initWithTitle:#"H"
message:#"yo"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[a show];
break;
case DiseasesFlu:
[UIView animateWithDuration:1.0 animations:^ {
}completion:^(BOOL finished) {
}];
break;
case DiseasesChickenPox:
[UIView animateWithDuration:1.0 animations:^ {
}completion:^(BOOL finished) {
}];
break;
case DiseasesSmallPox:
[UIView animateWithDuration:1.0 animations:^ {
}completion:^(BOOL finished) {
}];
break;
case DiseasesMeasels:
[UIView animateWithDuration:1.0 animations:^ {
}completion:^(BOOL finished) {
}];
break;
case DiseasesCommonCold:
[UIView animateWithDuration:1.0 animations:^ {
}completion:^(BOOL finished) {
}];
break;
}
}
To clear anything up, all the UIView animation and completion blocks are not completed. I am mainly focused on the first case. Is it because I have the first case as a 1? When I had it set to 0, it still did nothing.
The actual issue with everything, is when i press the first UIButton in my UIScrollView, I am able to press it, but what happens is the button does nothing regarding the IBAction method. It doesn't seem to be calling it at all.
Any help is appreciated.
OK, you are simply using the wrong argument to the action method. It should be:
- (IBAction) showDiseasesWithTag:(id)sender {
NSInteger senderTag = [sender tag];
switch (senderTag) {
...
}
}
Under iOS the target method can have one of three different signatures:
- (IBAction)actionMethod;
- (IBAction)actionMethod:(id)sender;
- (IBAction)actionMethod:(id)sender forEvent:(UIEvent *)event;
but never:
- (IBAction)actionMethod:(NSInteger)senderTag;
Related
I am trying to create an effect where a custom image in a UIBarButtonItem changes to another image. So far, I have been able to get the first image to dissolve with the following code. Can anyone suggest how I could get the second image to fade in at the same time?
//Create barbuttonitem in viewwillappear
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:CGRectMake(12, 0, 22, 22)];
[b addTarget:self action:#selector(menuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"firstimage.png"] forState:UIControlStateNormal];
UIBarButtonItem * myBarButton = [[UIBarButtonItem alloc] initWithCustomView:b];
//Animate in viewDidAppaer
[UIView animateWithDuration:1.0
delay:1.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.myBarButton.customView.alpha = 0;
}
completion:^(BOOL finished) {
UIImage *secondImage = [UIImage imageNamed:#"menu2.png"];
[self.myBarButton setImage:secondImage];//THis does not change image
self.myBarButton.customView.alpha = 1;//no animation
}];
You can use something like this
[UIView transitionWithView:self.myBarButton
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{ [self.myBarButton setImage:secondImage];}
completion:nil];
Try this code
#interface ViewController ()
{
UIBarButtonItem * myBarButton;
NSTimer *time;
UIButton *b;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:CGRectMake(12, 0, 22, 22)];
[b addTarget:self action:#selector(menuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"AddPlus_Yellow.png"] forState:UIControlStateNormal];
myBarButton = [[UIBarButtonItem alloc] initWithCustomView:b];
self.navigationItem.rightBarButtonItem=myBarButton;
time = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector:#selector(onTick:) userInfo: nil repeats:NO];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)onTick:(NSTimer *)timer {
[b setImage:[UIImage imageNamed:#"filter.png"] forState:UIControlStateNormal];
[time invalidate];
}
Actually There are four buttons and there is sub buttons for each button.once I click first button the second button have to move down by giving space for sub buttons but its not working even after setting scroll view.
bgsroll=[[UIScrollView alloc]init];
bgsroll.frame=CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
bgsroll.userInteractionEnabled=YES;
bgsroll.scrollEnabled=YES;
// bgsroll.backgroundColor=[UIColor grayColor];
bgsroll.showsVerticalScrollIndicator=YES;
bgsroll.contentSize=CGSizeMake(200, 2000);
[self.view addSubview:bgsroll];
happyBtn=[[UIButton alloc]init];
happyBtn.frame=CGRectMake(MainView.frame.size.width*0.12, MainView.frame.size.height*0.1, CGRectGetWidth(self.view.frame)/1.5, 40);
[happyBtn setTitle:#"Happiness" forState:UIControlStateNormal];
[happyBtn setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[happyBtn addTarget:self action:#selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside];
[bgsroll addSubview:happyBtn];
happyView=[[UIView alloc]init];
happyView.frame=CGRectMake(MainView.frame.size.width*0.26,CGRectGetHeight(happyBtn.frame)*2.2, CGRectGetWidth(MainView.frame)/1.6, CGRectGetHeight(MainView.frame)/3);
happyView.layer.cornerRadius=8.0;
happyView.layer.borderWidth=0.5;
happyView.layer.borderColor=[[UIColor blueColor]CGColor];
[bgsroll addSubview:happyView];
LettingGo=[[UIButton alloc]init];
LettingGo.frame=CGRectMake(happyView.frame.size.width*0.01,happyView.frame.size.height*0.1, CGRectGetWidth(happyView.frame)/1.05, 50);
[LettingGo setTitle:#"Letting go of negativity" forState:UIControlStateNormal];
LettingGo.titleLabel.numberOfLines=2;
LettingGo.titleLabel.adjustsFontSizeToFitWidth=YES;
[LettingGo setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[happyView addSubview:LettingGo];
LivingPresent=[[UIButton alloc]init];
LivingPresent.frame=CGRectMake(happyView.frame.size.width*0.01,LettingGo.frame.size.height*1.4, CGRectGetWidth(happyView.frame)/1.05, 40);
[LivingPresent setTitle:#"Living in the present" forState:UIControlStateNormal];
LivingPresent.titleLabel.adjustsFontSizeToFitWidth=YES;
[LivingPresent setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[happyView addSubview:LivingPresent];
happyView.hidden=YES;
CreateBtn=[[UIButton alloc]init];
CreateBtn.frame=CGRectMake(MainView.frame.size.width*0.12, MainView.frame.size.height*0.5, CGRectGetWidth(self.view.frame)/1.5, 40);
[CreateBtn setTitle:#"Happiness" forState:UIControlStateNormal];
[CreateBtn setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[CreateBtn addTarget:self action:#selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside];
[bgsroll addSubview:CreateBtn];
-(void)tapHappy:(id)selector{
happyView.hidden=!happyView.hidden;
}
By not working you mean it is not scrolling when the buttons are pushed? I find it easier to scroll elements creating a method that changes the coordinates of elements in subviews through animations. Something like this:
- (void)viewDidLoad {
expandButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[expandButton1 addTarget:self action:#selector(expandButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)expandButtonPressed:(id)sender{
if (expanded == NO) {
expanded = YES;
[UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:1];
image1.frame = CGRectMake(image1.frame.origin.x, image1.frame.origin.y+200.0f, image1.frame.size.width, image1.frame.size.height);
image.frame2 = CGRectMake(image2.frame.origin.x, image2.frame.origin.y+200.0f, image2.frame.size.width, image2.frame.size.height);
expandButton2.frame = CGRectMake(expandButton2.frame.origin.x, expandButton2.frame.origin.y+200.0f, expandButton2.frame.size.width, expandButton2.frame.size.height);
expandButton3.frame = CGRectMake(expandButton3.frame.origin.x, expandButton3.frame.origin.y+200.0f, expandButton3.frame.size.width, expandButton3.frame.size.height);
}completion:nil];
}
This question already has answers here:
How to stop unwanted UIButton animation on title change?
(24 answers)
Closed 4 months ago.
I am animating a view that is sliding down. In the view there is a simple UIButton. The view and the button have fixed widths and heights (I checked with adding a color as background). Although when use:
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Down the notification
CGRect notificationsRect = self.notificationContainerView.bounds;
notificationsRect.origin.y = 0;
notificationViewController.view.frame = notificationsRect;
} completion:^(BOOL finished) {
[notificationViewController didMoveToParentViewController:self];
self.currentNotification = notificationViewController;
}];
then the view slides down perfectly, expect for that the text in the UIButton kind of fades in. It starts really small and animates to the correct font size.
How can I disable the animation of the text in the UIButton?
Is the effect you have something similar to what is below? I tried to imitate your situation by having a view with a UIButton inside with some some text set for the title of the button. I then animated the button similarly to the way you did. As you can see there isn't really a fade happening on the text of my button inside my view that is sliding so I wonder if there is something else at play for you. I have the code I used below also to mock the situation.
- (void)viewDidLoad {
[super viewDidLoad];
self.myView = [[UIView alloc] initWithFrame:CGRectMake(200, 0, 100, 100)];
self.myView.backgroundColor = [UIColor greenColor];
UIButton *myButton= [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 80, 80)];
myButton.backgroundColor = [UIColor blueColor];
[myButton setTitle:#"fun times" forState:UIControlStateNormal];
[self.myView addSubview:myButton];
[self.view addSubview:self.myView];
UIButton *resetButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 300, 50, 50)];
resetButton.backgroundColor = [UIColor blackColor];
[resetButton addTarget:self action:#selector(reset) forControlEvents:UIControlEventTouchUpInside];
UIButton *goButton = [[UIButton alloc] initWithFrame:CGRectMake(300, 100, 50, 50)];
goButton.backgroundColor = [UIColor redColor];
[goButton addTarget:self action:#selector(go) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:goButton];
[self.view addSubview:resetButton];
}
- (void)reset {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Up the notification
CGRect notificationsRect = self.myView.bounds;
notificationsRect.origin.y = 0;
notificationsRect.origin.x = 200;
self.myView.frame = notificationsRect;
} completion:nil];
}
- (void)go {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Slide Down the notification
CGRect notificationsRect = self.myView.bounds;
notificationsRect.origin.y = 200;
notificationsRect.origin.x = 200;
self.myView.frame = notificationsRect;
} completion:nil];
}
Disable animations for UIButton using setAnimationsEnabled:(BOOL)enabled
Reference:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/index.html#//apple_ref/occ/clm/UIView/setAnimationsEnabled:
I'm trying to create a custom Alert View similar to this
I want to use a View Controller as the alert because the 'X' button on the top left corner must be there, and neither a regular UIAlertController or any pod I found can help me with that.
How can I achieve this?
Thank you.
For showing viewController as UIAlertView try this
SecondViewController *secondViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"SecondViewController"];
secondViewController.view.frame = self.view.frame;
[self.view addSubview:secondViewController.view];
secondViewController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
secondViewController.view.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
}];
try this..
#interface alertViewController ()
{
UILabel *labelTitle;
UIView *viewToShow,*viewAlert;
}
#end
#implementation alertViewController
- (void)viewDidLoad
{
[super viewDidLoad];
viewToShow = [[UIView alloc]initWithFrame:CGRectMake(0,0, 165*2, 400)];
[viewToShow setBackgroundColor:[UIColor whiteColor]];
[viewToShow setOpaque:NO];
[self.navigationController.view addSubview:viewToShow];
labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 165*2, 30)];
[[labelTitle layer] setCornerRadius:14];
labelTitle.text=#"TITLE";
[labelTitle setTextAlignment:NSTextAlignmentCenter];
[viewToShow addSubview:labelTitle];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(cancelButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"X" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:#"helvetica-neue" size:20]];
button.frame = CGRectMake(5,5,30, 40.0);
[viewToShow addSubview:button];
UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(10, 60,viewToShow.frame.size.width-20, 250)];
[textView setBackgroundColor:[UIColor lightGrayColor]];
[viewToShow addSubview:textView];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self
action:#selector(cancelButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:#"Continue" forState:UIControlStateNormal];
button1.frame = CGRectMake((viewToShow.frame.size.width/2)-40,viewToShow.frame.size.height-40,80,40);
[viewToShow addSubview:button1];
[[viewToShow layer] setCornerRadius:14];
CGPoint centerForCustomExportView;
if (UIDeviceOrientationIsLandscape((UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation]))
{
centerForCustomExportView = CGPointMake(([[UIScreen mainScreen]bounds].size.height>[[UIScreen mainScreen]bounds].size.width?[[UIScreen mainScreen]bounds].size.height:[[UIScreen mainScreen]bounds].size.width)/2, (([[UIScreen mainScreen]bounds].size.height>[[UIScreen mainScreen]bounds].size.width?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2));
}
else
{
centerForCustomExportView = CGPointMake([[UIScreen mainScreen]bounds].size.width/2, ([[UIScreen mainScreen]bounds].size.height-self.navigationController.navigationBar.frame.size.height)/2);
}
viewToShow.center = centerForCustomExportView;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotated:)name:UIDeviceOrientationDidChangeNotification object:nil];
}
try this.....
- (void)show
{
NSLog(#"show");
isShown = YES;
self.transform = CGAffineTransformMakeScale(0.1, 0.1);
self.alpha = 0;
[UIView beginAnimations:#"showAlert" context:nil];
[UIView setAnimationDelegate:self];
self.transform = CGAffineTransformMakeScale(1.1, 1.1);
self.alpha = 1;
[UIView commitAnimations];
}
I have created a Table-view in main ViewController once i touch the row it will take to the next view controller(DetailViewController) where it display the image and name of dish.And once i tap the image it should get larger but it is not getting bigger .The below code is which i have tried in DetailViewController.
DetailViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
imageView=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10,100, 100)];
CGRect textViewFrame = CGRectMake(10, 10, 300, 400);
textView = [[UITextView alloc] initWithFrame:textViewFrame];
textView.returnKeyType = UIReturnKeyDone;
textView.backgroundColor=[UIColor whiteColor];
textView.editable=NO;
textView.delegate = self;
[self.view addSubview:textView];
[textView addSubview:imageView];
textView.alpha = 0.9;
textView.font = [UIFont systemFontOfSize:15];
if(mrowno==0)
{
imageView.image=[UIImage imageNamed:#"Dosa.jpg"];
textView.text = #"\n\n\n\n\n\n\n\nDOSA\nDosa, ";
imageButton = [[UIButton alloc]init];
[imageButton setFrame:CGRectMake(0, 0, 100, 100)];
[imageButton addTarget:self action:#selector(imageTapped:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:imageButton];
}
Here is the method to make image larger
-(void)imageTapped:(UIButton*)in_sender
{
[UIView transitionWithView:in_sender.superview duration:0.8f options:UIViewAnimationOptionCurveLinear animations:^
{
[in_sender.superview setFrame:CGRectMake(0, 0, 300, 500)];
}
completion:^(BOOL finished)
{
}];
}
You can increase and decrease the size of image by pinch in and pinch out,If it required
-(void)viewDidLoad
{
UIPinchGestureRecognizer* pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinching:)];
[self.imageView addGestureRecognizer:pinch];
}
-(void)pinching:(UIGestureRecognizer*) recognizer
{
UIPinchGestureRecognizer* pinch=(UIPinchGestureRecognizer*) recognizer;
self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
pinch.scale=1;
NSLog(#"pinching");
}
You do not want to use transitionWithView. Instead, you want to use + animateWithDuration: delay:options:animations:completion: (or any of its shorter variants...)
[UIView animateWithDuration:0.3f delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
viewYouWantToAnimate.frame = CGRectMake(..., ..., ..., ...);
} completion:^{
//Add completion code here, or pass nil if you don't have anything to do.
}];
Edit: In case you were wondering what transitionWithView was used for... the AppleDocs have a great example of it adding animations for remove/addSubview methods (not frame modifications):
[UIView transitionWithView:containerView
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[fromView removeFromSuperview];
[containerView addSubview:toView];
} completion:NULL];