Status message damages main window in iPhone app

In my iOS application, when a button is clicked, an event is triggered, and a status message appears at the bottom of the screen informing the user about the success or failure of the operation.

A status message appears and disappears with the following code.

this will display a message

- (void) showMessage:(NSString*)text { CGRect frame = [[self view] frame]; if(statusView == nil) { messageViewWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, frame.size.height-29, frame.size.width, 0)]; statusView = [[StatusMessageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 0) Text:text andShowIndicator:NO]; [messageViewWindow addSubview:statusView]; [messageViewWindow makeKeyAndVisible]; } UILabel *label = [statusView getTextLabel]; UIImageView *imageView = [statusView getBackImageView]; CGSize stringSize = [text sizeWithFont:[Constants getLabelFont]]; int xCoordinate = (messageViewWindow.frame.size.width - stringSize.width)/2; [UIView beginAnimations:nil context:nil]; messageViewWindow.frame = CGRectMake(0, frame.size.height-59, frame.size.width, 30); statusView.frame = CGRectMake(0, 0, frame.size.width, 30); label.frame = CGRectMake(xCoordinate,5,stringSize.width,20); imageView.frame = CGRectMake(0, 0, frame.size.width, 30); [UIView commitAnimations]; [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(hideMessage) userInfo:nil repeats:NO]; } 

and it hides the message

 - (void) hideMessage { UILabel *label = [statusView getTextLabel]; UIImageView *imageView = [statusView getBackImageView]; CGRect labelFrame = label.frame; [UIView beginAnimations:nil context:nil]; messageViewWindow.frame = CGRectMake(0, [UIScreen mainScreen].applicationFrame.size.height-29, [UIScreen mainScreen].applicationFrame.size.width, 0); statusView.frame = CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, 0); label.frame = CGRectMake(labelFrame.origin.x, 0, labelFrame.origin.y, 0); imageView.frame = CGRectMake(0, 0, [UIScreen mainScreen].applicationFrame.size.width, 0); [UIView commitAnimations]; } 

it is strange that the message is displayed and disappears after two seconds, but the main window loses its functionality, for example, the ability to copy and paste.

Do you see something wrong in my methods? Is there any specific way that calls the animation code?

0
source share
2 answers

Since you write [messageViewWindow makeKeyAndVisible]; , you need to write [self.view.window makeKeyAndVisible] in hideMessage so that self.view.window handles all user interaction again.

0
source
  • This is because you are using UIWindow to display your view, which is not a good idea (it is not recommended to create UIWindows yourself, as described in the "Overview" section of the documentation.
  • In addition, you make your window a key window, but do not restore the standard window at the end when you hide it, thereby your problems with events later.
  • And finally, you are using legacy APIs for animations that are deprecated since iOS4 is missing (and I think you are not encoding iOS3 or earlier), you really have to use the new APIs that were introduced in iOS version 3 back now. In addition, this avoids the overhead of creating an NSTimer for this.

So, the code can become:

 - (void) showMessage:(NSString*)text { CGSize sz = self.view.window.frame.size; CGRect hiddenFrame = CGRectMake(0, sz.height-29, sz.width, 0); if(statusView == nil) { statusView = [[StatusMessageView alloc] initWithFrame:hiddenFrame text:text andShowIndicator:NO]; [self.window addSubview:statusView]; } UILabel *label = [statusView getTextLabel]; UIImageView *imageView = [statusView getBackImageView]; CGSize stringSize = [text sizeWithFont:[Constants getLabelFont]]; int xCoordinate = (sz.width - stringSize.width)/2; label.frame = CGRectMake(xCoordinate,5,stringSize.width,20); imageView.frame = CGRectMake(0, 0, frame.size.width, 30); [UIView animateWithDuration:1.f animations:^{ statusView.frame = CGRectMake(0, sz.height-59, frame.size.width, 30); } completion:^{ // When show animation is done, schedule the start of the hide animation with a delay of 2 seconds [UIView animateWithDuration:1.f delay:2.f options:0 animation:^{ statusView.frame = hiddenFrame; } completion:nil]; }]; } 
0
source

Source: https://habr.com/ru/post/1438446/


All Articles