Show a UIViewController as Alert View in iOS - ios

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];
}

Related

Second UIView Overlapped on First UIView when i change simulator at portrait to landscape mode

Hi I am new for iOS and I am create two UIviews programmatically on my main ViewController and here I have added two button on my UIviews they are Next and Back buttons
Here when I click Next button I make to move first UIview to second UIview (like how we move one view controller to another view controller). When I click Back button I am pushing back to second UIview to first UIview using UIview animations
Here my main problem is: when I change first UIview orientation at portrait to landscape mode, second UIview is overlapped on my first UIview. That is my main issue and I am not understand why this problem is coming/ Please help me someone.
My code:
#import "ViewController.h"
#interface ViewController (){
UIView * MyFisrtView;
UIView * MySecondView;
UIButton * GoNext;
UIButton * GoBack;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyFisrtView = [[UIView alloc] init];
MyFisrtView.backgroundColor = [UIColor orangeColor];
MyFisrtView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:MyFisrtView];
MySecondView = [[UIView alloc] init];
MySecondView.backgroundColor = [UIColor lightGrayColor];
MySecondView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:MySecondView];
NSDictionary * HeaderDictionary = NSDictionaryOfVariableBindings(MyFisrtView,MySecondView);
//Appliying Autolayouts for FirstView
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"H:|-0-[MyFisrtView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|-0-[MyFisrtView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
//Appliying Autolayouts for SecondView
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"H:|-0-[MySecondView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:#"V:|-0-[MySecondView]-0-|"]
options:0
metrics:nil
views:HeaderDictionary]];
GoNext = [UIButton buttonWithType: UIButtonTypeRoundedRect];
GoNext.frame = CGRectMake(50, 50, 100, 18);
[GoNext setTitle:#"Next" forState:UIControlStateNormal];
[GoNext addTarget:self action:#selector(GoNext:) forControlEvents:UIControlEventTouchUpInside];
GoNext.backgroundColor = [UIColor lightGrayColor];
[MyFisrtView addSubview:GoNext];
GoBack = [UIButton buttonWithType: UIButtonTypeRoundedRect];
GoBack.frame = CGRectMake(50, 50, 100, 18);
[GoBack setTitle:#"Back" forState:UIControlStateNormal];
[GoBack addTarget:self action:#selector(GoBack:) forControlEvents:UIControlEventTouchUpInside];
GoBack.backgroundColor = [UIColor orangeColor];
[MySecondView addSubview:GoBack];
MyFisrtView.hidden = NO;
MySecondView.hidden = YES;
}
-(void)GoNext:(id)sender{
MySecondView.hidden = NO;
MySecondView.frame=CGRectMake(248, 0, self.view.frame.size.width, self.view.frame.size.height); // starting visible position
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[MySecondView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; // final visible position
}
completion:nil];
}
-(void)GoBack:(id)sender{
MySecondView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); // starting visible position
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[MySecondView setFrame:CGRectMake(MyFisrtView.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)]; // final visible position
}
completion:nil];
}
#end
This is because of the screen size changes from landscape to portrait. For example, in portrait mode height will be large and width will be small when compare it with landscape mode.
So, what you have to do is, you have to fix separate height and width for portrait and landscape and also change x and y position for different orientation.
And also, from your code I didn't see any change orientation delegates. If you want to fix your issue, you have to implement those delegates.
First of all comment out your Constraints and make the modifications as below
#interface ViewController (){
UIView * MyFisrtView;
UIView * MySecondView;
UIButton * GoNext;
UIButton * GoBack;
BOOL isSecondShown;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyFisrtView = [[UIView alloc] initWithFrame:self.view.frame];
MyFisrtView.backgroundColor = [UIColor orangeColor];
MyFisrtView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:MyFisrtView];
MySecondView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x + self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
MySecondView.backgroundColor = [UIColor lightGrayColor];
MySecondView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:MySecondView];
GoNext = [UIButton buttonWithType: UIButtonTypeRoundedRect];
GoNext.frame = CGRectMake(50, 50, 100, 18);
[GoNext setTitle:#"Next" forState:UIControlStateNormal];
[GoNext addTarget:self action:#selector(GoNext:) forControlEvents:UIControlEventTouchUpInside];
GoNext.backgroundColor = [UIColor lightGrayColor];
[MyFisrtView addSubview:GoNext];
GoBack = [UIButton buttonWithType: UIButtonTypeRoundedRect];
GoBack.frame = CGRectMake(50, 50, 100, 18);
[GoBack setTitle:#"Back" forState:UIControlStateNormal];
[GoBack addTarget:self action:#selector(GoBack:) forControlEvents:UIControlEventTouchUpInside];
GoBack.backgroundColor = [UIColor orangeColor];
[MySecondView addSubview:GoBack];
// MyFisrtView.hidden = NO;
// MySecondView.hidden = YES;
}
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
if(isSecondShown){
[MyFisrtView setFrame:CGRectMake(MySecondView.frame.origin.x - MySecondView.frame.size.width, MySecondView.frame.origin.y, MySecondView.frame.size.width, MySecondView.frame.size.height)];
[MySecondView setFrame:self.view.frame];
}
else{
[MyFisrtView setFrame:self.view.frame];
[MySecondView setFrame:CGRectMake(self.view.frame.origin.x + self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
}
}
-(void)GoNext:(id)sender{
// MySecondView.hidden = NO;
MySecondView.frame=CGRectMake(248, 0, self.view.frame.size.width, self.view.frame.size.height); // starting visible position
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[MySecondView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; // final visible position
}
completion:^(BOOL finished){
isSecondShown = YES;
}];
}
-(void)GoBack:(id)sender{
// MySecondView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); // starting visible position
[MyFisrtView setFrame:CGRectMake(MySecondView.frame.origin.x - MySecondView.frame.size.width, MySecondView.frame.origin.y, MySecondView.frame.size.width, MySecondView.frame.size.height)];
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[MySecondView setFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)]; // final visible position
[UIView animateWithDuration:0.5f animations:^{
[MyFisrtView setFrame:self.view.frame];
}];
}
completion:^(BOOL finished){
isSecondShown = NO;
}];
}
#end

ViewController transition by button Touch (no NVController)

I have created a button programmatically and added a selector to it
(touched) which when touchup inside should transist to the next view
controller (LocationVC) with no navigationController between. I have
added in storyboard a second view controller and named its class
"LocationVC2. Somehow nothing happens then a black screen. Any help?
//
// ViewController.h
// NanCom4
//
// Created by Daniel Habshush on 8/30/15.
// Copyright © 2015 Daniel Habshush. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIButton *circle1;
#property (nonatomic, strong) IBOutlet UIButton *circle2;
#property (nonatomic, strong) IBOutlet UIButton *circle3;
#property (nonatomic, strong) IBOutlet UIButton *circle4;
#property (nonatomic, strong) IBOutlet UIButton *circle5;
#end
- (void)viewDidLoad {
[super viewDidLoad];
circle1 = [[UIButton alloc] init];
[circle1 setFrame:CGRectMake(128, 247, 100, 100)];
[circle1 setIsAccessibilityElement:YES];
[circle1 addTarget:self action:#selector(pressed3:) forControlEvents:UIControlEventTouchUpInside];
circle1.backgroundColor = [UIColor clearColor];
[circle1 setImage:[UIImage imageNamed:#"Circle6.png"] forState:UIControlStateNormal];
[self.view addSubview:circle1];
circle2 = [[UIButton alloc] init];
[circle2 setFrame:CGRectMake(55, 170, 120, 120)];
[circle2 setIsAccessibilityElement:YES];
[circle2 addTarget:self action:#selector(touch:) forControlEvents:UIControlEventTouchUpInside];
circle2.backgroundColor = [UIColor clearColor];
[circle2 setTitle:#"Orit" forState:UIControlStateNormal];
[circle2 setBackgroundImage: [UIImage imageNamed:#"pie2.png"] forState:UIControlStateNormal];
[self.view addSubview:circle2];
circle3 = [[UIButton alloc] init];
[circle3 setFrame:CGRectMake(55, 300, 120, 120)];
[circle3 setIsAccessibilityElement:YES];
[circle3 setAlpha:0];
circle3.backgroundColor = [UIColor clearColor];
[circle3 setTitle:#"DANI" forState:UIControlStateNormal];
[circle3 setBackgroundImage: [UIImage imageNamed:#"pie3.png"] forState:UIControlStateNormal];
[self.view addSubview:circle3];
circle4 = [[UIButton alloc] init];
[circle4 setFrame:CGRectMake(185, 300, 120, 120)];
[circle4 setIsAccessibilityElement:YES];
[circle4 setAlpha:0];
circle4.backgroundColor = [UIColor clearColor];
[circle4 setTitle:#"DANI" forState:UIControlStateNormal];
[circle4 setBackgroundImage: [UIImage imageNamed:#"pie5.png"] forState:UIControlStateNormal];
[self.view addSubview:circle4];
circle5 = [[UIButton alloc] init];
[circle5 setFrame:CGRectMake(185, 170, 120, 120)];
[circle5 setIsAccessibilityElement:YES];
[circle5 setAlpha:0];
circle5.backgroundColor = [UIColor clearColor];
[circle5 setTitle:#"DANI" forState:UIControlStateNormal];
[circle5 setBackgroundImage: [UIImage imageNamed:#"pie4.png"] forState:UIControlStateNormal];
[self.view addSubview:circle5];
}
- (void)pressed3:(UIButton*)sender {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.circle1.alpha = 0.2;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.circle2.alpha = 1;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelay:0.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.circle3.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelay:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.circle4.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelay:0.75];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.circle5.alpha = 1;
[UIView commitAnimations];
}
- (void)touch:(id)sender
{
LocationVC *newVC = [LocationVC new];
[self presentViewController:newVC animated:YES completion:nil];
}
#end

IOS Button touch up inside - First touch = Action1 and second touch = Action2// loop between the two

Hi all
I have attached below code. Upon launch, the view controller displays
a logo in the center and four main user buttons on the bottom.I try to
achieve that if the first button got touched up inside (Called Lists),
the logo fades and the center circle displays the name (Lists) of the
first button and shows around it buttons with different submenu
points, in the same time the 4 main (initial) buttons fade and are
only seen at a minimal (low Alpha). I want to achieve that if the
faded (half visible)main button gets touched again initial view is
restored with logo and only the main four menu buttons. Meaning, first
touch shows sub menus and its animations and second click returns to
initial view. Again clicked shows again submenu and so on. How can i
do that?
Thanks Dnai
//
// ViewController.h
// NanCom1
//
// Created by Daniel Habshush on 8/13/15.
// Copyright © 2015 Daniel Habshush. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, retain) IBOutlet UIImageView* logo;
#property (nonatomic, retain) IBOutlet UIButton* button1;
#property (nonatomic, retain) IBOutlet UIButton* button2;
#property (nonatomic, retain) IBOutlet UIButton* button3;
#property (nonatomic, retain) IBOutlet UIButton* button4;
#property (nonatomic, retain) IBOutlet UIButton* listMain;
#property (nonatomic, retain) IBOutlet UIButton* shoppingList;
#property (nonatomic, retain) IBOutlet UIButton* todoList;
#end
//
// ViewController.m
// NanCom1
//
// Created by Daniel Habshush on 8/13/15.
// Copyright © 2015 Daniel Habshush. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
{
UIDynamicAnimator* _animator;
UIGravityBehavior* _gravity;
UICollisionBehavior* _collision;
}
#synthesize logo, button1, button2, button3, button4, listMain, todoList, shoppingList;
- (void)viewDidLoad {
[super viewDidLoad];
logo = [[UIImageView alloc] init];
UIImage *logoPic = [UIImage imageNamed:#"Logo3.png"];
logo.image=logoPic;
logo.frame = CGRectMake(100, 200, 175, 175);
[logo setAlpha:0];
[self.view addSubview:logo];
[UIImageView beginAnimations:nil context:nil];
[UIImageView setAnimationDuration:3.0];
[UIImageView setAnimationDelay:0.5];
[UIImageView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.logo.alpha = 1.0;
[UIImageView commitAnimations];
button1 = [[UIButton alloc] init];
[button1 setFrame:CGRectMake(5, 2, 90, 90)];
[button1 setIsAccessibilityElement:YES];
button1.backgroundColor = [UIColor clearColor];
[button1 setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[button1 setTitle:#"Lists" forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[button1 addTarget:self action:#selector(pressed:) forControlEvents:UIControlEventTouchUpInside];
[button1 setEnabled:YES];
[self.view addSubview:button1];
button2 = [[UIButton alloc] init];
[button2 setFrame:CGRectMake(85, 2, 90, 90)];
[button2 setIsAccessibilityElement:YES];
button2.backgroundColor = [UIColor clearColor];
[button2 setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[button2 setTitle:#"Cirlce" forState:UIControlStateNormal];
[button2.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[button2 setEnabled:YES];
[button2 addTarget:self action:#selector(pressed2:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
button3 = [[UIButton alloc] init];
[button3 setFrame:CGRectMake(160, 2, 90, 90)];
button3.backgroundColor = [UIColor clearColor];
[button3 setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[button3 setTitle:#"Location" forState:UIControlStateNormal];
[button3.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[button3 setEnabled:YES];
[button2 addTarget:self action:#selector(pressed3:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
button4 = [[UIButton alloc] init];
[button4 setFrame:CGRectMake(250, 2, 90, 90)];
button4.backgroundColor = [UIColor clearColor];
[button4 setTitle:#"Menu" forState:UIControlStateNormal];
[button4.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[button4 setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[button4 setEnabled:YES];
[button2 addTarget:self action:#selector(pressed4:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button4];
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_gravity = [[UIGravityBehavior alloc] initWithItems:#[button1, button2, button3, button4]];
[_animator addBehavior:_gravity];
_collision = [[UICollisionBehavior alloc]
initWithItems:#[button1, button2, button3, button4]];
_collision.translatesReferenceBoundsIntoBoundary = YES;
[_animator addBehavior:_collision];
UIDynamicItemBehavior* itemBehaviour4 = [[UIDynamicItemBehavior alloc] initWithItems:#[button4]];
itemBehaviour4.elasticity = 0.4;
itemBehaviour4.resistance = 0.65;
[_animator addBehavior:itemBehaviour4];
UIDynamicItemBehavior* itemBehaviour3 = [[UIDynamicItemBehavior alloc] initWithItems:#[button3]];
itemBehaviour3.elasticity = 0.45;
itemBehaviour3.resistance = 0.5;
[_animator addBehavior:itemBehaviour3];
UIDynamicItemBehavior* itemBehaviour2 = [[UIDynamicItemBehavior alloc] initWithItems:#[button2]];
itemBehaviour2.elasticity = 0.5;
itemBehaviour2.resistance = 0.3;
[_animator addBehavior:itemBehaviour2];
UIDynamicItemBehavior* itemBehaviour1 = [[UIDynamicItemBehavior alloc] initWithItems:#[button1]];
itemBehaviour1.elasticity = 0.37;
itemBehaviour1.resistance = 0.2;
[_animator addBehavior:itemBehaviour1];
}
- (void)pressed:(UIButton*)button1 {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button1.alpha = 0.3;
self.button2.alpha = 0.3;
self.button3.alpha = 0.3;
self.button4.alpha = 0.3;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.logo.alpha = 0;
[UIView commitAnimations];
listMain = [[UIButton alloc] init];
[listMain setFrame:CGRectMake(100, 200, 175, 175)];
listMain.backgroundColor = [UIColor clearColor];
[listMain setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[listMain setTitle:#"Lists" forState:UIControlStateNormal];
[listMain.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:35]];
[listMain setAlpha:0];
[self.view addSubview:listMain];
todoList = [[UIButton alloc] init];
[todoList setFrame:CGRectMake(60, 145, 75, 75)];
todoList.backgroundColor = [UIColor clearColor];
[todoList setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[todoList setTitle:#"ToDo" forState:UIControlStateNormal];
[todoList.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[todoList setAlpha:0];
[self.view addSubview:todoList];
shoppingList = [[UIButton alloc] init];
[shoppingList setFrame:CGRectMake(10, 245, 75, 75)];
shoppingList.backgroundColor = [UIColor clearColor];
[shoppingList setBackgroundImage:[UIImage imageNamed:#"Circle.png"] forState:UIControlStateNormal];
[shoppingList setTitle:#"Shopping" forState:UIControlStateNormal];
[shoppingList.titleLabel setFont:[UIFont fontWithName:#"Avenir" size:13]];
[shoppingList setAlpha:0];
[self.view addSubview:shoppingList];
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.listMain.alpha = 1;
self.todoList.alpha = 1;
self.shoppingList.alpha = 1;
[UIView commitAnimations];
}
- (void)pressed2:(UIButton*)button2 {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button2.alpha = 1;
self.button1.alpha = 1;
self.button3.alpha = 1;
self.button4.alpha = 1;
self.listMain.alpha = 0;
self.todoList.alpha = 0;
self.shoppingList.alpha = 0;
[UIView commitAnimations];
}
- (void)pressed3:(UIButton*)button3 {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.listMain.alpha = 0;
self.todoList.alpha = 0;
self.shoppingList.alpha = 0;
self.logo.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button2.alpha = 1;
self.button1.alpha = 1;
self.button3.alpha = 1;
self.button4.alpha = 1;
[UIView commitAnimations];
}
- (void)pressed4:(UIButton*)button4 {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button2.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button1.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button3.alpha = 1;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDelay:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn ];
self.button4.alpha = 1;
[UIView commitAnimations];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Why isn't my button working?

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;

How to fit image in UITextView and Tap to get larger

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];

Resources