How to switch View Controllers in iOS? - ios

I've looked all over Google & Stack Overflow, but I haven't found one clear answer. How do I switch to the TOCViewController after tapping the UIButton? I'm currently using "presentViewController" but it just freezes on tapping the button. Thanks in advance for any help.
#import "SplashLoginViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "TOCViewController.h"
#interface SplashLoginViewController ()
#end
#implementation SplashLoginViewController
- (void) displayTOC {
TOCViewController *toc = [[TOCViewController alloc] init];
[self presentViewController:toc animated:YES completion:nil];
}
- (void)initializeSplashView
{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)];
backgroundView.backgroundColor = [UIColor colorWithRed:52.0/255.0 green:170.0/255.0 blue:220.0/255.0 alpha:1.0];
backgroundView.tag = 1;
backgroundView.layer.zPosition = 99.0f;
[self.view addSubview:backgroundView];
UILabel *codeworksLabelView = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width, self.view.bounds.size.height / 2.0 - 25.0, self.view.bounds.size.width, 50.0)];
codeworksLabelView.text = #"Codeworks";
codeworksLabelView.textAlignment = NSTextAlignmentCenter;
codeworksLabelView.textColor = [UIColor whiteColor];
codeworksLabelView.font = [UIFont fontWithName:#"Montserrat" size:30.0];
codeworksLabelView.tag = 2;
codeworksLabelView.layer.zPosition = 100.0f;
[self.view addSubview:codeworksLabelView];
}
- (void)initializeLoginView
{
CGRect loginFrame = CGRectMake(50.0, 100.0, self.view.bounds.size.width - 100.0, 30.0);
CGRect passwordFrame = CGRectMake(50.0, 180.0, self.view.bounds.size.width - 100.0, 30.0);
UITextField *usernameInput = [[UITextField alloc] init];
usernameInput.frame = loginFrame;
usernameInput.placeholder = #"Username";
usernameInput.font = [UIFont fontWithName:#"Montserrat" size:15.0];
usernameInput.returnKeyType = UIReturnKeyNext;
usernameInput.tag = 3;
usernameInput.autocorrectionType = UITextAutocorrectionTypeNo;
UITextField *passwordInput = [[UITextField alloc] init];
passwordInput.frame = passwordFrame;
passwordInput.placeholder = #"Password";
passwordInput.font = [UIFont fontWithName:#"Montserrat" size:15.0];
passwordInput.returnKeyType = UIReturnKeyDone;
passwordInput.tag = 4;
passwordInput.secureTextEntry = YES;
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0, 29.0, usernameInput.frame.size.width, 1.0);
bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
CALayer *borderCopy = [CALayer layer];
borderCopy.frame = CGRectMake(0.0, 29.0, passwordInput.frame.size.width, 1.0);
borderCopy.backgroundColor = [UIColor blackColor].CGColor;
[usernameInput.layer addSublayer:bottomBorder];
[passwordInput.layer addSublayer:borderCopy];
[self.view addSubview:usernameInput];
[self.view addSubview:passwordInput];
UILabel *signInButton = [[UILabel alloc] initWithFrame:CGRectMake(0, 270, self.view.bounds.size.width, 60.0)];
signInButton.font = [UIFont fontWithName:#"Montserrat" size:15.0];
signInButton.text = #"Sign In";
signInButton.textColor = [UIColor blackColor];
signInButton.tag = 5;
signInButton.textAlignment = NSTextAlignmentCenter;
CALayer *topButtonBorder = [CALayer layer];
topButtonBorder.frame = CGRectMake(0, 0, self.view.bounds.size.width, 1);
topButtonBorder.backgroundColor = [UIColor blackColor].CGColor;
CALayer *bottomButtonBorder = [CALayer layer];
bottomButtonBorder.frame = CGRectMake(0, 59, self.view.bounds.size.width, 1);
bottomButtonBorder.backgroundColor = [UIColor blackColor].CGColor;
[signInButton.layer addSublayer:topButtonBorder];
[signInButton.layer addSublayer:bottomButtonBorder];
[self.view addSubview:signInButton];
UIView *optionsDivide = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - 0.5, self.view.bounds.size.height - 45, 1, 20)];
optionsDivide.backgroundColor = [UIColor blackColor];
[self.view addSubview:optionsDivide];
UIButton *tos = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *pp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
tos.frame = CGRectMake(self.view.bounds.size.width / 2 - 115.5 , self.view.bounds.size.height - 65, 110, 60);
pp.frame = CGRectMake(self.view.bounds.size.width / 2 + 0.5 , self.view.bounds.size.height - 65, 110, 60);
[tos setTitle:#"Terms of Service" forState:UIControlStateNormal];
[pp setTitle:#"Privacy Policy" forState:UIControlStateNormal];
pp.titleLabel.textAlignment = NSTextAlignmentLeft;
tos.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:12.0];
pp.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:12.0];
[tos addTarget:self action:#selector(displayTOC:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:tos];
[self.view addSubview:pp];
}
- (void)animateSplashScreen
{
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.view viewWithTag:2].frame = CGRectMake(0.0, self.view.bounds.size.height / 2.0 - 25.0, self.view.bounds.size.width, 50.0);
}
completion:^(BOOL finished){
[self initializeLoginView];
[UIView animateWithDuration:0.5
delay:1.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.view viewWithTag:1].frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 65.0);
[self.view viewWithTag:2].frame = CGRectMake(0.0, 15.0, self.view.bounds.size.width, 50.0);
((UILabel *)[self.view viewWithTag:2]).transform = CGAffineTransformScale(((UILabel *)[self.view viewWithTag:2]).transform, 0.6, 0.6);
}
completion:^(BOOL finished){
((UILabel *)[self.view viewWithTag:2]).font = [UIFont fontWithName:#"Montserrat" size:30.0];
}];
}];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initializeSplashView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self animateSplashScreen];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//CGPoint locationPoint = [[touches anyObject] locationInView:self.view];
//CGPoint viewPoint = [[self.view viewWithTag:5] convertPoint:locationPoint fromView:self.view];
//if ([[self.view viewWithTag:5] pointInside:viewPoint withEvent:event]) {
// [self.view viewWithTag:5].frame = CGRectMake(self.view.bounds.size.width / 2 - 62.5, 254.5, 130.0, 60.0);
//}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//[self.view viewWithTag:5].frame = CGRectMake(self.view.bounds.size.width / 2 - 67.5, 249.5, 130.0, 60.0);
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
//[self.view viewWithTag:5].frame = CGRectMake(self.view.bounds.size.width / 2 - 67.5, 249.5, 130.0, 60.0);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end

I copy-pasted your code into a file and found a warning on this line [tos addTarget:self action:#selector(displayTOC:) forControlEvents:UIControlEventTouchUpInside]; The warning says,
Undeclared selector 'displayTOC:'
When I ran the code and tapped the TOC button, it didn't just freeze, it crashed with the error:
-[ViewController displayTOC:]: unrecognized selector sent to instance ...
These should be strong hints on what is wrong in your code. When you ask people about your code, you should reveal any and all warnings, and crashes.
Note, a method named displayTOC is not the same as a method named displayTOC:
By the way, the animation you did is really cool. Good job!

Related

Add customview in navigation bar like whatsapp

I want to create custom navigation bar like WhatsApp uses to display call indicator in the application as given below.
I have successfully added view like above but it's not responsive because I am not able to detect touch on status bar. I can touch only part below "Touch to return to call".
Code is as given below.
#property (nonatomic) UIView *navigationBarTopView;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
if (_navigationBarTopView == nil) {
_navigationBarTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, window.frame.size.width, 60.0)];
[_navigationBarTopView setBackgroundColor:[UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _navigationBarTopView.frame.size.height - 15, window.frame.size.width, 10)];
[label setText:#"Touch to return to call"];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
[label setTextAlignment:NSTextAlignmentCenter];
[_navigationBarTopView addSubview:label];
//The setup code (in viewDidLoad in your view controller)
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[_navigationBarTopView addGestureRecognizer:singleFingerTap];
UITapGestureRecognizer *singleFingerTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[label addGestureRecognizer:singleFingerTap1];
}
[window addSubview:_navigationBarTopView];
I have also tried to add touch on status bar as give below but it doesn't work.
How do I detect touches on UIStatusBar/iPhone
Also navigation bar is not coming downside. I have tried to set keyWindow frame. But that is also not working.
UIStatusBar has higher priority than your application's window, so you won't get any touch event through status bar.
To get touch event through status bar you need a window with higher window level than UIStatusBar.
Try below code:
#import "AppDelegate.h"
#interface AppDelegate ()
#property (nonatomic) UIWindow *buttonWindow;
#property (nonatomic) UIWindow *textWindow;
#property (nonatomic) CGFloat callViewHeight;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
if (#available(iOS 11.0, *)) {
if (([[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0)) {
self.callViewHeight = 55.0f;
} else {
self.callViewHeight = 35.0f;
}
} else {
self.callViewHeight = 35.0f;
}
return YES;
}
-(void)showVideoCallButton{
self.textWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
self.textWindow.windowLevel = self.window.windowLevel + 1;
self.textWindow.hidden = FALSE;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
view.backgroundColor = [UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1];
view.clipsToBounds = TRUE;
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
lblName.textAlignment = NSTextAlignmentCenter;
[lblName setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
lblName.text = #"";
UILabel *lblTouch = [[UILabel alloc] initWithFrame:CGRectMake(0, self.callViewHeight - 20.0f, [UIScreen mainScreen].bounds.size.width, 20.0f)];
lblTouch.textAlignment = NSTextAlignmentCenter;
[lblTouch setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
lblTouch.text = #"Touch to return to call";
lblTouch.textColor = UIColor.whiteColor;
[view addSubview:lblTouch];
[view addSubview:lblName];
[self.textWindow addSubview:view];
self.buttonWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.callViewHeight)];
self.buttonWindow.windowLevel = UIWindowLevelStatusBar + 1;
self.buttonWindow.hidden = FALSE;
UIButton *button = [[UIButton alloc] initWithFrame:lblName.bounds];
[button setTitle:#"" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTouched) forControlEvents:UIControlEventTouchUpInside];
[self.buttonWindow addSubview:button];
self.textWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
self.buttonWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
[UIView animateWithDuration:0.25
animations:^{
self.textWindow.transform = CGAffineTransformIdentity;
self.buttonWindow.transform = CGAffineTransformIdentity;
if (#available(iOS 11.0, *)) {
if (([[[UIApplication sharedApplication] delegate] window].safeAreaInsets.top > 20.0)) {
self.window.frame = CGRectMake(0, self.callViewHeight - 40.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 40.0f);
} else {
self.window.frame = CGRectMake(0, self.callViewHeight - 20.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 20.0f);
}
} else {
self.window.frame = CGRectMake(0, self.callViewHeight - 20.0f, self.window.bounds.size.width, self.window.bounds.size.height - self.callViewHeight + 20.0f);
}
}];
}
-(void)hideVideoCallButton{
[UIView animateWithDuration:0.25
animations:^{
self.textWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
self.buttonWindow.transform = CGAffineTransformMakeTranslation(0, -self.callViewHeight);
self.window.frame = [UIScreen mainScreen].bounds;
} completion:^(BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{
self.buttonWindow.hidden = TRUE;
self.buttonWindow = nil;
self.textWindow.hidden = TRUE;
self.textWindow = nil;
});
}];
}
-(void)buttonTouched{
NSLog(#"Button Touched");
}
#end
I tried in singleViewcontroller and UITabbar Controller also, the sample project I attached here, for status bar Tap action I followed Flar answer
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
[self createDummyView];
});
}
-(void)createDummyView{
if (_navigationBarTopView == nil) {
float statusBarHeight = [self statusBarHeight];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
_navigationBarTopView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, self.navigationController.navigationBar.frame.size.height + statusBarHeight)];
[_navigationBarTopView setBackgroundColor:[UIColor colorWithRed:76.0/255.0 green:217.0/255.0 blue:100.0/255.0 alpha:1]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _navigationBarTopView.frame.size.height - 15,width, 10)];
[label setText:#"Touch to return to call"];
[label setTextColor:[UIColor whiteColor]];
[label setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]];
[label setTextAlignment:NSTextAlignmentCenter];
[label setUserInteractionEnabled:YES];
[_navigationBarTopView addSubview:label];
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[_navigationBarTopView addGestureRecognizer:singleFingerTap];
[self.navigationController.view addSubview:_navigationBarTopView];
}
-(float) statusBarHeight
{
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
return MIN(statusBarSize.width, statusBarSize.height);
}
-(void)handleSingleTap:(UITapGestureRecognizer*)recognizer{
NSLog(#"recognizer == %#", recognizer.view.tag);
}

Display UILabel in UIView programatically or using Storyboard

I have added a UIView and now, i want to show a UILabel. However, it does not get displayed. Can someone help me out please ?
- (IBAction)infoButtonClicked:(id)sender {
myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5, 10, self.view.frame.size.width, 100)];
[myView addSubview:label];
[self.view addSubview: myView];
[UIView animateWithDuration:0.3/1.5 animations:^{
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
myView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3/2 animations:^{
myView.transform = CGAffineTransformIdentity;
label.text=#"The title";
}];
}];
}];
//The setup code to detect single touch
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(oneTap:)];
[myView addGestureRecognizer:singleFingerTap];
}
try this
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(5, 10, myView.frame.size.width, 100)];
label.text = #"sample TExt";
label.numberOfLines = 1;
label.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; // or UIBaselineAdjustmentAlignCenters, or UIBaselineAdjustmentNone
label.adjustsFontSizeToFitWidth = YES;
label.adjustsLetterSpacingToFitWidth = YES;
label.minimumScaleFactor = 10.0f/12.0f;
label.clipsToBounds = YES;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;
[myView addSubview:label];

Dismiss custom alert view iOS

I'm setting up a subclass of UIView to roll my own UIAlertView style view. I've got everything set up properly with displaying the view, but I'm at a bit of a loss as to how to dismiss the view properly. Specifically, when a user taps on the button in the view, it needs to animate out of the main view. This is the code for the view itself:
+ (void)showCustomAlertWithTitle:(NSString *)titleString andMessage:(NSString *)messageString inView:(UIView *)view andButton1Title: (NSString *)button1Title andButton2Title: (NSString *)button2Title
{
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
CGRect windowFrame = window.frame;
[view setAlpha:0.5f];
UIColor *buttonColor = [UIColor colorWithRed:0/255.0f green:130/255.0f blue:216/255.0f alpha:1];
UIColor *titleColor = [UIColor colorWithRed:0/255.0f green:153/255.0f blue:102/255.0f alpha:1];
// Shade
UILabel *shadeWindow = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, windowFrame.size.width, windowFrame.size.height)];
shadeWindow.backgroundColor = [UIColor blackColor];
shadeWindow.alpha = 0.50f;
// Define size and origin of alert box
float alertBoxHeight = 225;
float alertBoxWidth = 200;
float alertBoxXorigin = windowFrame.size.width / 2 - (alertBoxWidth / 2);
float alertBoxYorigin = windowFrame.size.height / 2 - (alertBoxHeight / 2);
// Initialize background
UIView *alertBackground = [[UIView alloc] initWithFrame:CGRectMake(alertBoxXorigin, alertBoxYorigin, alertBoxWidth, alertBoxHeight)];
alertBackground.layer.cornerRadius = 5.0f;
[alertBackground.layer setMasksToBounds:YES];
alertBackground.layer.backgroundColor = [UIColor whiteColor].CGColor;
// Title Label
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, alertBoxWidth, 40)];
titleLabel.text = titleString;
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.textColor = titleColor;
titleLabel.layer.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.05f].CGColor;
[titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:20.0]];
// Title Divider
UILabel *titleDivider = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, alertBoxWidth, 1.0)];
titleDivider.backgroundColor = [UIColor grayColor];
titleDivider.alpha = 0.5f;
// Alert Message Text
UITextView *alertMessage = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, alertBoxWidth, alertBoxHeight - 90)];
alertMessage.text = messageString;
alertMessage.layer.backgroundColor = [UIColor whiteColor].CGColor;
[alertMessage setFont:[UIFont fontWithName:#"Avenir" size:15.0]];
alertMessage.textAlignment = NSTextAlignmentCenter;
alertMessage.textColor = [UIColor grayColor];
[alertMessage setEditable:NO];
// Button 1
UIButton *button1 = [[UIButton alloc] init];
UIButton *button2 = [[UIButton alloc] init];
[button1 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
if (button2Title == nil)
{
button1.frame = CGRectMake(10, alertBoxHeight - 50, alertBoxWidth - 20, 40);
}
else
{
button1.frame = CGRectMake(10, alertBoxHeight - 50, (alertBoxWidth / 2) - 20, 40);
button2.frame = CGRectMake(alertBoxWidth / 2 + 10, alertBoxHeight - 50, (alertBoxWidth / 2) - 20, 40);
}
button1.layer.backgroundColor = buttonColor.CGColor;
[button1 setTitle:button1Title forState:UIControlStateNormal];
[button1.titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:15.0f]];
button1.layer.cornerRadius = 2.5f;
[button1.layer setMasksToBounds:YES];
// Button 2
button2.layer.backgroundColor = buttonColor.CGColor;
[button2 setTitle:button2Title forState:UIControlStateNormal];
[button2.titleLabel setFont:[UIFont fontWithName:#"AvenirNext-Bold" size:15.0f]];
button2.layer.cornerRadius = 2.5f;
[button2.layer setMasksToBounds:YES];
// Bounce Implementation
alertBackground.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
{
alertBackground.transform = CGAffineTransformIdentity;
}
completion:^(BOOL finished)
{
// do something once the animation finishes, put it here
}];
[window addSubview:view];
[window addSubview:alertBackground];
[view addSubview:shadeWindow];
[view bringSubviewToFront:alertBackground];
[alertBackground addSubview:button1];
[alertBackground addSubview:titleLabel];
[alertBackground addSubview:titleDivider];
[alertBackground addSubview:alertMessage];
[alertBackground addSubview:button2];
[alertBackground bringSubviewToFront:titleDivider];
/* [[[customAlerts sharedInstance] subViewArray] addObject:alertBackground];
[[[customAlerts sharedInstance] subViewArray] addObject:view];
[[[customAlerts sharedInstance] subViewArray] addObject:window];*/
}
When button1 is tapped, for instance, I need to have it animate the view out of the superView and remove it from the stack. I'm not sure how to handle this. Does anyone have any ideas?
To permanently remove a view, I generally use the UIView instance method removeFromSuperview. followed by a line setting the relevant variable to nil:
[myAlertView removeFromSuperview];
myAlertView = nil;
In your case then, I think you need to move the view off the screen by animating the its bounds property, then use the above couple of lines to remove any references to it.
Just found the answer here:
Dismiss view controller from #selector without creating seperate method
Had to download some custom classes but it worked:
[button1 addEventHandler:^(id sender, UIEvent *event)
{
[UIView animateWithDuration:0.25f animations:^{
[alertBackground setAlpha:0.0f];
[shadeWindow setAlpha:0.0f];
[window setAlpha:0.0f];
}];
} forControlEvent:UIControlEventTouchUpInside];
Custom classes can be found here:
https://github.com/ZeR0-Wu/JTTargetActionBlock

Delete function doesn't works when animation begins ?

I am trying to implement ,long press and delete functionality of apple, myself.
I almost achieved but, there is something i could not figure out.
When shaking animation starts delete button do not take the touches.
How to handle while it is shaking deleting the view.
Here is my code;
self.frame = CGRectMake(0, 0, 1024, 768);
[self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];
circleView = [[UIView alloc] initWithFrame:CGRectMake(50,55,90,90)];
circleView.layer.cornerRadius = 45;
circleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1];
circleView.layer.borderColor = [[UIColor blackColor] CGColor];
circleView.layer.borderWidth = 4;
UILabel* circleIndex = [[UILabel alloc] init];
circleIndex.frame = CGRectMake(30, 25, 40, 40);
[circleIndex setFont:[UIFont fontWithName:#"Arial-BoldMT" size:40]];
[circleIndex setText:#"A"];
[circleView addSubview:circleIndex];
UIView *exitView = [[UIView alloc] initWithFrame:CGRectMake(78,5,20,20)];
exitView.layer.cornerRadius = 10;
exitView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:1];
exitView.layer.borderColor = [[UIColor whiteColor] CGColor];
exitView.layer.borderWidth = 2;
UILabel* exitLabel = [[UILabel alloc] init];
exitLabel.frame = CGRectMake(5, 0.5, 25, 20);
[exitLabel setFont:[UIFont fontWithName:#"Arial-BoldMT" size:25]];
[exitLabel setTextColor:[UIColor whiteColor]];
[exitLabel setBackgroundColor:[UIColor clearColor]];
[exitLabel setText:#"-"];
[exitView addSubview:exitLabel];
[circleView addSubview:exitView];
UIView *alertView = [[UIView alloc]initWithFrame:CGRectMake(40, 80, 944, 600)];
[exitView setUserInteractionEnabled:YES];
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:#selector(handleLongPress:)];
[circleView addGestureRecognizer:longPress];
UITapGestureRecognizer *singlePress =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSinglePress:)];
[exitView addGestureRecognizer:singlePress];
//[longPress release];
[circleView bringSubviewToFront:exitView];
[alertView addSubview:circleView];
[self addSubview:alertView];
}
return self;
}
//The event handling method
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {
//[circleView removeFromSuperview];
[self shakeView:recognizer.view];
}
}
- (void)handleSinglePress:(UITapGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if ( recognizer.state == UIGestureRecognizerStateEnded ) {
[recognizer.view.superview removeFromSuperview];
}
}
- (void)shakeView:(UIView *)viewToShake
{
CGFloat t = 2.0;
CGAffineTransform translateRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);
viewToShake.transform = translateLeft;
[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:200.0];
viewToShake.transform = translateRight;
} completion:^(BOOL finished) {
if (finished) {
[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform = CGAffineTransformIdentity;
} completion:NULL];
}
}];
}
Use in the options parameter of your animation UIViewAnimationOptionAllowUserInteraction

UIElements in subclass with uiscrollview are non responsive

I have a class that provides a custom menu for all its subclasses, but since i started using the text field have run to the keyboard problem so i use the normal method and embed the subclass in a resizing uiscrollview. the problem is that the scrollview doesnt cover the menu that belongs to its super class.
The idea that came to mind is to create a scrollview in the super class and inside the scrollview put the menu. I run it and everything seemed to be okay until i couldn't interact with anything on the screen other than the navigation bar and the menu which lead me to believe that maybe the subclass uielements are not subviews of the scrollview.
The problem even expands since in the subclasses I create uielements in the storyboard and in code. My question would be how to make that all uielements created in the subclass and added in self.view become subviews of the superclass' scrollview.
SUPERCLASS
- (void)viewDidLoad
{
[super viewDidLoad];
//_scrollViewMain = [[UIScrollView alloc] initWithFrame:self.view.bounds];
//[self.view addSubview:_scrollViewMain];
_slideButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(slide)];
[self.navigationItem setRightBarButtonItem:_slideButton];
_menuView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - 44, self.view.bounds.size.width, 44)];
_menuView.backgroundColor = [UIColor colorWithRed:63.0/255.0 green:63.0/255.0 blue:63.0/255.0 alpha:1.0];
[self.view addSubview:_menuView];
//[_scrollViewMain addSubview:_menuView];
UIView *redFrameView = [[UIView alloc] initWithFrame:CGRectMake(0, _menuView.frame.size.height / 2, _menuView.frame.size.width, _menuView.frame.size.height / 2)];
[redFrameView setBackgroundColor:[UIColor colorWithRed:170.0/255.0 green:0.0 blue:0.0 alpha:0.5]];
[_menuView addSubview:redFrameView];
_menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_menuButton setImage:[UIImage imageNamed:#"19-gear.png"] forState:UIControlStateNormal];
[_menuButton addTarget:self action:#selector(menu) forControlEvents:UIControlEventTouchUpInside];
_menuButton.frame = CGRectMake(16.0, 9.0, 26.0, 26.0);
_addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_addButton setImage:[UIImage imageNamed:#"13-plus.png"] forState:UIControlStateNormal];
[_addButton addTarget:self action:#selector(add) forControlEvents:UIControlEventTouchUpInside];
_addButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
_addButton.userInteractionEnabled = NO;
_addButton.alpha = 0.0;
_editButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_editButton setImage:[UIImage imageNamed:#"187-pencil.png"] forState:UIControlStateNormal];
[_editButton addTarget:self action:#selector(edit) forControlEvents:UIControlEventTouchUpInside];
_editButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
_editButton.userInteractionEnabled = NO;
_editButton.alpha = 0.0;
_deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_deleteButton setImage:[UIImage imageNamed:#"11-x.png"] forState:UIControlStateNormal];
[_deleteButton addTarget:self action:#selector(delete) forControlEvents:UIControlEventTouchUpInside];
_deleteButton.frame = CGRectMake(16.0, 15.0, 14.0, 14.0);
_deleteButton.userInteractionEnabled = NO;
_deleteButton.alpha = 0.0;
_downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_downloadButton setImage:[UIImage imageNamed:#"57-download.png"] forState:UIControlStateNormal];
[_downloadButton addTarget:self action:#selector(download) forControlEvents:UIControlEventTouchUpInside];
_downloadButton.frame = CGRectMake(16.0, 15.0, 11.0, 14.0);
_downloadButton.userInteractionEnabled = NO;
_downloadButton.alpha = 0.0;
_uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_uploadButton setImage:[UIImage imageNamed:#"56-cloud.png"] forState:UIControlStateNormal];
[_uploadButton addTarget:self action:#selector(upload) forControlEvents:UIControlEventTouchUpInside];
_uploadButton.frame = CGRectMake(16.0, 15.0, 21.0, 14.0);
_uploadButton.userInteractionEnabled = NO;
_uploadButton.alpha = 0.0;
_shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_shareButton setImage:[UIImage imageNamed:#"124-bullhorn.png"] forState:UIControlStateNormal];
[_shareButton addTarget:self action:#selector(share) forControlEvents:UIControlEventTouchUpInside];
_shareButton.frame = CGRectMake(16.0, 15.0, 21.0, 14.0);
_shareButton.userInteractionEnabled = NO;
_shareButton.alpha = 0.0;
[_menuView addSubview:_addButton];
[_menuView addSubview:_editButton];
[_menuView addSubview:_deleteButton];
[_menuView addSubview:_downloadButton];
[_menuView addSubview:_uploadButton];
[_menuView addSubview:_shareButton];
[_menuView addSubview:_menuButton];
_isMenuVisible = NO;
}
FIRST attempt but the scrolldidn't included the menuview in the superclass
- (void)viewDidLoad
{
[super viewDidLoad];
[self registerForKeyboardNotifications];
self.title = [_exercise valueForKey:kExerciseName];
[super ableAdd];
[super ableDownload];
[super ableShare];
if ([[_exercise valueForKey:kExerciseIsDefault] boolValue]) {
[super ableUpload];
[super ableDelete];
}
_doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
_nameLabel.text = [_exercise valueForKey:kExerciseName];
_nameLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
_categoryLabel.text = [[_exercise valueForKey:kExerciseEType] valueForKey:kExerciseTypeName];
_categoryLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
_gearLabel.text = [[_exercise valueForKey:kGear] valueForKey:kGearName];
_gearLabel.textColor = [UIColor colorWithRed:128.0/255.0 green:128.0/255.0 blue:128.0/255.0 alpha:1.0];
_addRecordButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 30) / 2, 7, 30, 30)];
[_addRecordButton setImage:[UIImage imageNamed:#"85-trophy.png"] forState:UIControlStateNormal];
[_addRecordButton addTarget:self action:#selector(addRecord) forControlEvents:UIControlEventTouchUpInside];
[_toolView addSubview:_addRecordButton];
_showChartButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 50, 7, 30, 30)];
[_showChartButton setImage:[UIImage imageNamed:#"16-line-chart.png.png"] forState:UIControlStateNormal];
[_showChartButton addTarget:self action:#selector(showChart) forControlEvents:UIControlEventTouchUpInside];
[_toolView addSubview:_showChartButton];
_numberOfPages = 3;
_currentPage = 1;
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 174, 320, self.view.bounds.size.height - 262)];
_scrollView.pagingEnabled = YES;
_scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width * _numberOfPages, _scrollView.bounds.size.height);
_scrollView.delegate = self;
_scrollView.showsHorizontalScrollIndicator = NO;
_textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
_textView.contentSize = CGSizeMake(_scrollView.bounds.size.width, _scrollView.bounds.size.height);
_textView.text = [_exercise valueForKey:kExerciseOverview];
_textView.editable = NO;
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(_scrollView.bounds.size.width * 1, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
_tableView.contentSize = CGSizeMake(_scrollView.bounds.size.width, _scrollView.bounds.size.height);
_tableView.dataSource = self;
_tableView.delegate = self;
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(handleLongPress:)];
longPress.minimumPressDuration = 2.0;
longPress.delegate = (id<UIGestureRecognizerDelegate>)self;
[_tableView addGestureRecognizer:longPress];
_movieView = [[UIWebView alloc] initWithFrame:CGRectMake(_scrollView.bounds.size.width * 2, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height)];
_movieView.opaque = YES;
_movieView.backgroundColor = [UIColor whiteColor];
_movieView.autoresizesSubviews = YES;
[_movieView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[_exercise valueForKey:kExerciseUrlAddress]]]];
[_scrollView addSubview:_textView];
[_scrollView addSubview:_tableView];
[_scrollView addSubview:_movieView];
[_scrollView scrollRectToVisible:CGRectMake(_scrollView.bounds.size.width * 1, 0, _scrollView.bounds.size.width, _scrollView.bounds.size.height) animated:NO];
[_scrollViewMain addSubview:_scrollView];
//[self.view addSubview:_scrollView];
_pageControl.numberOfPages = _numberOfPages;
_pageControl.currentPage = _currentPage;
_records = [[NSMutableArray alloc] init];
_records = [[NSArray arrayWithArray:[[_exercise valueForKey:kExerciseERecords] allObjects]] mutableCopy];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:kExerciseRecordRecordDate ascending:NO];
[_records sortUsingDescriptors:[NSArray arrayWithObject:sort]];
}
- (void)viewDidDisappear:(BOOL)animated
{
if (self.editing) {
[super setPan];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[_tableView setEditing:editing animated:animated];
[super setPan];
if (self.navigationItem.rightBarButtonItem == nil) {
self.navigationItem.rightBarButtonItem = _doneButton;
}
_addRecordButton.enabled = !editing;
_showChartButton.enabled = !editing;
if (![[_exercise valueForKey:kExerciseIsDefault] boolValue]) {
[super ableUpload];
[super ableDelete];
_textView.editable = editing;
}
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
_scrollViewMain.contentInset = contentInsets;
_scrollViewMain.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, _activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, _activeField.frame.origin.y-kbSize.height);
[_scrollViewMain setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
_scrollViewMain.contentInset = contentInsets;
_scrollViewMain.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
_activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
_activeField = nil;
}

Resources