Custom NSMenuItem-views glitch when scrolling NSMenu on low resolution screen - drawrect

I'm having some glitchy bugs when I'm using my menu bar application on a low resolution screen. Please see screenshots below as illustration! Because of the reduced height of the lo-res screen my total NSMenu does not fit in the screen, and so automatically a scrollview appears so i can still get to all the different items.
The problem is that my cutsom NSMenuItems, which thus have custom NSViews with their own draw [drawRect:(NSRect)dirtyRect] method, are behaving glitchy when scrolling down, and back up. It's as if there are some drawing calls missing. I unfortunately do not know how to detect when the NSMenu is effectively scrolling, or simply to access this automagically created scrollview on the NSMenu class...
Any help much appreciated !
the drawing code of my view in my NSMenuItem.
- (void)drawRect:(NSRect)dirtyRect
{
if (self.enabled)
{
if (self.isHighlighted)
{
[self colorRectHighlighted:dirtyRect];
[self drawTextInRect:dirtyRect withState:CustomCheckMenuItemViewStateHiglighted];
[self drawStateIconWithColor:[NSColor whiteColor]];
}
else
{
[self drawTextInRect:dirtyRect withState:CustomCheckMenuItemViewStateNormal];
AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
if(appDelegate.menuIsInDarkMode)
{
[self drawStateIconWithColor:[NSColor colorWithWhite:1.0 alpha:0.9]];
}
else
{
[self drawStateIconWithColor:[NSColor selectedMenuItemIndicatorColor:([NSColor currentControlTint]==NSBlueControlTint)?YES:NO]];
}
}
}
else
{
[self drawTextInRect:dirtyRect withState:CustomCheckMenuItemViewStateNormal];
AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate];
if(appDelegate.menuIsInDarkMode)
{
[self drawStateIconWithColor:[NSColor colorWithRed:136/256.0 green:136/256.0 blue:136/256.0 alpha:0.9]];
}
else
{
[self drawStateIconWithColor:[NSColor colorWithWhite:0 alpha:0.1]];
}
}
}
EDIT:
I initialise the NSMenuItems with the sliders as this:
self.tiltSliderViewController = [[SlidingViewController alloc] initWithNibName:#"SlidingViewController" bundle:nil];
self.tiltSliderViewController.delegate = self;
[tiltSliderMenuItem setView:[self.tiltSliderViewController view]];
self.rotationSliderViewController = [[SlidingViewController alloc] initWithNibName:#"SlidingViewController" bundle:nil];
self.rotationSliderViewController.delegate = self;
[rotationSliderMenuItem setView:[self.rotationSliderViewController view]];
The viewcontrollers used here have the following implementation:
#interface SlidingViewController ()
#end
#implementation SlidingViewController
- (IBAction)valueChanged:(id)sender
{
float slider_value = [_slider floatValue];
[_slider setToolTip:[NSString stringWithFormat:#"%ld",
_slider.integerValue]];
[_delegate sliderValueChanged:slider_value sender:self];
}
#end
screenshots:
NORMAL
DOWN
UP AGAIN = GLITCH!!

Related

How to restore keyWindow (NSView - Appkit)

I have some code which opens a modal window (itself composed from a few views) and makes it closed when we click anywhere on it.
here is some part of code:
int myFunc()
{
// Create views
NSPanel *panel = ...;
CustomNSTextView * textView = ... ;
CustomNSImageView * imageView = ...;
[view addSubview:textView];
[view addSubview:imageView];
[panel setContentView:view];
[[NSApplication sharedApplication] runModalForWindow:panel];
NSView* parentView = [view superview];
[[parentView window] makeFirstResponder:parentView];
[textView release];
[imageView release];
[view release];
[panel release];
}
#implementation CustomNSTextView : NSTextView
- (void) mouseDown:(NSEvent *)theEvent
{
#pragma unused(theEvent)
[[NSApplication sharedApplication] stopModal];
}
#implementation CustomNSImageView : NSImageView
- (void) mouseDown:(NSEvent *)theEvent
{
#pragma unused(theEvent)
[[NSApplication sharedApplication] stopModal];
}
The bug is after the window is closed, the application (that launched the modal window), won't receive any key event anymore (while it still was as the modal window was still open).
Only after I refocus on the application, it will receive key events again.
Please anyone gives me some idea, I could not find something relevant on the net.
Thanks
Nathaniel

Why do my touch event not work?

I have declared a void touchesbegan method in my controller but it doesn't work! i don't know why. i have an image view which i plan to click it to move to next controller. so i set the touches began method. i have linked the image view in the xib file but when i click the image. nothing happens. Please help, Thanks.
ViewController.h
#import <UIKit/UIKit.h>
#interface imageViewViewController : UIViewController
{
IBOutlet UIImageView *testing;
}
#property(nonatomic, retain) IBOutlet UIImageView *testing;
#end
ViewController.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if(touch.view == testing)
{
TestViewController *testviewcontroller = [[TestViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:testviewcontroller animated:YES];
}
}
#end
P.S.
here i tried another method using tap gestures.
testing is the name of my image view. as u can see i comment out the ns log in the imagedidtapped method. it works til that point. however when i tried to navigate it out to another page it fails.
- (void)viewDidLoad
{
UITapGestureRecognizer *tapRecognizer;
[testing setTag:0];
[testing setUserInteractionEnabled:TRUE];
tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageViewDidTapped:)] autorelease];
tapRecognizer.numberOfTapsRequired = 1;
[testing addGestureRecognizer:tapRecognizer];
[self.view addSubview:testing];
[testing release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)imageViewDidTapped:(UIGestureRecognizer *)aGesture {
UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)aGesture;
UIImageView *tappedImageView = (UIImageView *)[tapGesture view];
switch (tappedImageView.tag) {
case 0:
//NSLog(#"UIImageView 1 was tapped");
[self navigate];
break;
case 1:
NSLog(#"UIImageView 2 was tapped");
break;
default:
break;
}
}
-(void)navigate
{
TestViewController *testviewcontroller = [[TestViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:testviewcontroller animated:YES];
}
The problem is that by default userInteractionEnabled is NO for a UIImageView so you won't get any touches. Set it to YES.
Also the message handling for touchesBegan is really complicated. You'll be much happier if you attach a UITapGestureRecognizer to the image view.
EDIT: Now you say your touches handling is working, but the navigation is not taking place. So let's concentrate on this part of your code:
-(void)navigate
{
TestViewController *testviewcontroller = [[TestViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:testviewcontroller animated:YES];
}
Put logging in there to make sure navigate is being called! If it isn't being called, you need to figure out why your other code is not running and calling it. If it is being called, then the problem is probably that self.navigationController is nil, i.e. you are not inside a navigation interface to start with.

UIButton not working in second time (objective-C)

I have a button in my first screen for loading a small game here is the code:
- (IBAction)playButtonPressed:(id)sender
{
[self startGameWithConfig:[RootViewController singleplayerGameConfig]];
NSLog(#"play again");
}
and inside of my game page I have a button to back to the first screen when I used this button I can back but my playButtonPressed not working any more, it print the NSLog but
the button it's not loading the game any more
here is my return button:
- (IBAction)return:(id)sender {
RootViewController *b = [[RootViewController alloc] init];
[self presentModalViewController:b animated:YES];
}
would you please help me to this implementation
Thanks in advance!
and here is my startGamingWithConfig
- (void) startGameWithConfig:(GameConfig *)config
{
// fade in the loading screen to hide load time
[_loadingView setHidden:NO];
[_loadingView setAlpha:0.0f];
[UIView animateWithDuration:0.2f
delay:0.0f
options:UIViewAnimationOptionCurveLinear
animations:^{
[_loadingView setAlpha:0.0f];
}
completion:^(BOOL finished){
CardsViewCtrl* newController = [[CardsViewCtrl alloc] init];
newController.gameConfig = config;
AppDelegate *delegate = (AppDelegate *)[[UIApplication
sharedApplication] delegate];
[delegate.navController pushFadeInViewController:newController
animated:YES];
}];
}
It is a bit hard for me to understand where this code is since there are no headings but it looks like when you are attempting to go back to the RootViewController you are creating a new one and pushing that on the stack. You want to make sure that your topViewController is removed, not adding more views.
So for a starter try to replace return method:
- (IBAction)return:(id)sender {
RootViewController *b = [[RootViewController alloc] init];
[self presentModalViewController:b animated:YES];
}
with this:
- (IBAction)goBack:(id)sender {
[self dismissModalViewController];
}
I am not sure how you have your view hierarchy setup, but if you have a working navigationController than try using [self.navigationController popNavigationController:YES];

dismissing the keyboard from a UITextField,UITextView as a subview of UIScrollView?

I have an application with UIScrollView added as a subview of UIView. i have added UITextField,UITextview as a subView of UIScrollView .I want to dismiss the keyboard when i tap in the scroll view. how can i do this?
Just add UITapGestureRecognizer
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[scr addGestureRecognizer:singleTap];
}
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
//Get touch point
CGPoint touchPoint=[gesture locationInView:scr];
//Hide keyBoard
[self.view endEditing:YES];
}
In iOS 7, you can achieve this easily.
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
Try this,
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:tapGesture];
[tapGesture release];
}
-(void)dismissKeyboard
{
[txtNotes resignFirstResponder];
[textView resignFirstResponder];
}
When I added the gesture to a subclass of UIScrollView, I was having problems with the various gestures in my view tree interfering with each other, such as being able to click on subviews, scroll the view, and have the keyboard dismiss in all cases. I came up with this solution, which can be setup from a superclass of UIScrollView or from a UIViewController.
The DismissKeyboardTapGesture class uses ARC, works with any text fields under the view, and doesn't take over any clicks from subviews like buttons. Also takes advantage of iOS7 scrolling effect to dismiss keyboard.
Setting up from UISScrollView superclass:
_dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self];
or from UIViewController:
_dismissKeyboard = [[DismissKeyboardTapGesture alloc] initWithView:self.view];
Here is the class:
#interface DismissKeyboardTapGesture : NSObject <UIGestureRecognizerDelegate>
#end
#implementation DismissKeyboardTapGesture
- (id)initWithView:(UIView *)view
{
self = [super init];
if (self) {
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTap:)];
singleTap.cancelsTouchesInView = NO;
singleTap.delegate = self;
[view addGestureRecognizer:singleTap];
if ([view respondsToSelector:#selector(setKeyboardDismissMode:)]) {
// Bonus effect to dismiss keyboard by scrolling
((UIScrollView *)view).keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
}
}
return self;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
// Don't stop any existing gestures in our view from working
if (otherGestureRecognizer.view == gestureRecognizer.view) {
return YES;
}
return NO;
}
- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer
{
// Close keyboard for any text edit views that are children of the main view
[gestureRecognizer.view endEditing:YES];
}
#end

Loading bar over status bar?

I've seen several apps do this, and i've been wondering how it's done. If you look at the Wunderkit app, when it loads something, there's a blue animated bar that slides down to cover the status bar until it finishes loading. How is this done?
you can subclass UIWindow.
And set it windowLevel = UIWindowLevelStatusBar;
code:
#interface CHLoadingWindow : UIWindow
#implementation CHLoadingWindow
- (id)init
{
self = [super init];
if (self) {
self.windowLevel = UIWindowLevelStatusBar;
// then draw your UI
}
return self;
}
#end
use it:
CHLoadingWindow *loading = [[CHLoadingWindow alloc] init];
[loading makeKeyAndVisible];
discover it:
[loading release];
loading = nil;
[[[[UIApplication sharedApplication] windows] objectAtIndex:0] makeKeyWindow];