iOS8 keyboard confusion, why root view's hitTest method be triggered? - ios8

When touches on the keyboard area, the root view's method be triggered:
(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
I am very confused,anyone can help me?

In your applicationWillEnterForeground in AppDelegate, put this code.
It works for me, specially with KLCPopup
- (void)applicationWillEnterForeground:(UIApplication *)application{
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
if (!IS_OS_8_OR_LATER) return;
[UIApplication.sharedApplication.windows enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIWindow *w, NSUInteger idx, BOOL *stop) {
if (!w.opaque && [NSStringFromClass(w.class) hasPrefix:#"UIText"]) {
// The keyboard sometimes disables interaction. This brings it back to normal.
BOOL wasHidden = w.hidden;
w.hidden = YES;
w.hidden = wasHidden;
*stop = YES;
}
}];}

This problem will appear in:
1、you changed keywindow‘s rootViewController;
2、enter background and return to the foreground;
So,restore UITextEffectsWindow can fixed it after your change everytime.
void TSRestoreKeyboardWindow(void)
{
if (!TSSystemVersionGreaterThanIOS8()) return;
[UIApplication.sharedApplication.windows enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIWindow *w, NSUInteger idx, BOOL *stop) {
if (!w.opaque && [NSStringFromClass(w.class) hasPrefix:#"UIText"]) {
// The keyboard sometimes disables interaction. This brings it back to normal.
BOOL wasHidden = w.hidden;
w.hidden = YES;
w.hidden = wasHidden;
*stop = YES;
}
}];
}

Related

The right way to make a continuously redrawn Metal NSView

I'm learning Metal and Cocoa and trying to make a boilerplate application as a platform for future experiments. As part of the process, I'm implementing a view which will redraw itself (or, more accurately, contents of its CAMetalLayer) on 60fps. Also for educational purposes Im avoiding MTKView (for "learning Cocoa part"). Here's an abbreviated code snippet of how I'm tackling the problem:
#implementation MyMetalView // which is a subclass of NSView
- (BOOL) isOpaque {
return YES;
}
- (NSViewLayerContentsRedrawPolicy) layerContentsRedrawPolicy {
return NSViewLayerContentsRedrawOnSetNeedsDisplay;
}
- (CALayer *) makeBackingLayer {
// create CAMetalLayer with default device
}
- (BOOL) wantsLayer {
return YES;
}
- (BOOL) wantsUpdateLayer {
return YES;
}
- (void) displayLayer:(CALayer *)layer {
id<MTLCommandBuffer> cmdBuffer = [_commandQueue commandBuffer];
id<CAMetalDrawable> drawable = [((CAMetalLayer *) layer) nextDrawable];
[cmdBuffer enqueue];
[cmdBuffer presentDrawable:drawable];
// rendering
[cmdBuffer commit];
}
#end
int main() {
// init app, window and MyMetalView instance
// invocation will call [myMetalViewInstance setNeedsDisplay:YES]
[NSTimer scheduledTimerWithTimeInterval:1./60. invocation:setNeedsDisplayInvokation repeats:YES];
[NSApp run];
return 0;
}
Is it the right way to do what I want? Or have I chosen a long and not recommended approach?
It is strongly preferred to use CVDisplayLink rather than a generic NSTimer to drive animations that need to match the refresh rate of the display.
You'll want to create an ivar or property to hold a CVDisplayLinkRef:
CVDisplayLinkRef displayLink;
Then, when your view is going onto the screen and you want to start animating, you'll create, configure, and start your display link:
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, self);
CVDisplayLinkStart(displayLink);
The display link callback should be a static function. It will be invoked at the beginning of the display's v-blank period (on modern displays where there is no physical v-blank, this still happens at a regular 60Hz cadence):
static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
{
[(MyMetalView *)displayLinkContext setNeedsDisplay:YES];
return kCVReturnSuccess;
}
When your view leaves the display, or if you want to pause, you can release the display link and nil it out:
CVDisplayLinkRelease(displayLink);
following #warrenm solution adding dispatch_sync to refresh and other minor :
#import "imageDrawer.h"
#import "image/ImageBuffer.h"
#import "common.hpp"
#implementation imageDrawer {
CVDisplayLinkRef displayLink;
}
CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeStamp* now, const CVTimeStamp* outputTime, CVOptionFlags flagsIn, CVOptionFlags* flagsOut, void* displayLinkContext)
{
dispatch_sync(dispatch_get_main_queue(), ^{
[(__bridge imageDrawer*)displayLinkContext setNeedsDisplay:YES];
});
return kCVReturnSuccess;
}
-(void)setContDisplay {
CVDisplayLinkCreateWithActiveCGDisplays(&displayLink);
CVDisplayLinkSetOutputCallback(displayLink, &MyDisplayLinkCallback, (__bridge void*)self);
CVDisplayLinkStart(displayLink);
}
-(void)awakeFromNib {
[self setContDisplay];
}
- (void)drawRect:(NSRect)rect {
[super drawRect:rect];
int w=rect.size.width, h=rect.size.height;
// do the drawing...
}
#end

how to stop runaway flashing NSButton / NSTimer (IOS or OSX ok)

I'm trying to make an NSButton (but I could use the technique for iOS too, so any answers are welcome).
I have a button where hiding/enabling is turned on and off:
-(void)textDidChange:(NSNotification *)notification
{
timerStatus = 0;
timerTest = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(changeButtonState:)
userInfo:nil
repeats:YES];
}
-(void) changeButtonState:(id) sender {
NSLog(#"%s", __FUNCTION__);
if (timerStatus == 2) return;
if (timerStatus == 0) {
timerStatus = 1;
saveButton.enabled = YES;
saveButton.hidden = NO;
} else {
timerStatus = 0;
saveButton.enabled = NO;
saveButton.hidden = YES;
}
}
The button blinks ok, but after it's associated method is fired (in this case a save action), I want the timer to stop and the button to stop blinking. It's this last part that's giving me a headache.. Any help appreciated.
- (IBAction)saveItemNotes:(id)sender
{
NSLog(#"%s", __FUNCTION__);
<do my save stuff here>
timerStatus = 2;
[timerTest invalidate];
timerTest = nil;
}
Just invalidate your timer in that same method where you change the button state.
Better yet, don't do it this way.
Use Core Animation instead if you want to draw attention to an interface item.

Hiding the dock icon without hiding the menu bar

I use the ideas in this thread to hide the dock icon of my app optionally. If the dock icon is shown after all, the menu bar should be shown too. Only with Jiulong's answer I haven't been able to make this work. The menu bar is still hidden.
So basically 'Application is agent' is set to '1' in the InfoPList, and this code is used :
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"LaunchAsAgentApp"]) {
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetSystemUIMode(kUIModeNormal, 0);
[[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:#"com.apple.dock" options:NSWorkspaceLaunchDefault additionalEventParamDescriptor:nil launchIdentifier:nil];
[[NSApplication sharedApplication] activateIgnoringOtherApps:TRUE];
}
So why doesn't the menu bar show up, until I hide and refocus the app? Is there any fix for this? I saw that the 'Quick Search Box' for mac app doesn't show the menu bar upon launching either...
EDIT : I contacted Apple, and they gave me a carbon and a non-carbon solution. Given a new project with 'Application is Agent' set to 'YES' in the Plist file, then this code could be used in the AppDelegate class :
#define USE_CARBON 0
//
// Note: NSLogDebug is defined in the projects pre-compiled (.pch) file
//
#implementation AppDelegate
{
BOOL show_icon;
}
// Application will finish launching
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
NSLogDebug();
NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
if (![[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) {
// exit(173);
}
#if 1
show_icon = YES;
#else
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *hasDockIconDefaultsKey = #"Has Dock Icon?";
// note: toggles value on each run (normally set from user pref pannel)
show_icon = [userDefaults boolForKey:hasDockIconDefaultsKey];
[userDefaults setBool:!show_icon forKey:hasDockIconDefaultsKey];
#endif // if 1
if (show_icon) {
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp setPresentationOptions:NSApplicationPresentationDefault];
[NSMenu setMenuBarVisible:NO];
[NSMenu setMenuBarVisible:YES];
}
[NSApp activateIgnoringOtherApps:YES];
} // applicationWillFinishLaunching
// Application did finish launching
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSLogDebug();
// Insert code here to initialize your application
if (show_icon) {
#if USE_CARBON
ProcessSerialNumber psn = {0, kCurrentProcess};
OSStatus returnCode = TransformProcessType(&psn, kProcessTransformToForegroundApplication);
if (noErr != returnCode) {
NSLog(#"TransformProcessType error: %d (0x%0X)", returnCode, returnCode);
}
ProcessSerialNumber psnx = {0, kNoProcess};
GetNextProcess(&psnx);
SetFrontProcess(&psnx);
#else // if 0
NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
NSRunningApplication * menuBarOwningApplication = [sharedWorkspace menuBarOwningApplication];
(void) [menuBarOwningApplication activateWithOptions:NSApplicationActivateIgnoringOtherApps];
#endif
[self performSelector:#selector(setFront) withObject:nil afterDelay:0.];
}
} // applicationDidFinishLaunching
// Close app when main window is closed
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return (YES);
}
- (void)setFront;
{
#if USE_CARBON
ProcessSerialNumber psn = {0, kCurrentProcess};
SetFrontProcess(&psn);
#else // if USE_CARBON
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
#endif // USE_CARBON
}
#end
Note that I filed a bug report too.
Here's a Swift version of the non-carbon solution :
func applicationWillFinishLaunching(_ notification: Notification) {
if showIcon {
NSApp.setActivationPolicy(.regular)
NSApp.presentationOptions = []
NSMenu.setMenuBarVisible(false)
NSMenu.setMenuBarVisible(true)
}
NSApp.activate(ignoringOtherApps: true)
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
NSApplication.shared.activate(ignoringOtherApps: true)
if showIcon {
let workspace = NSWorkspace.shared
let application = workspace.menuBarOwningApplication
application?.activate(options: .activateIgnoringOtherApps)
self.perform(#selector(activate), with: nil, afterDelay: 0.0)
}
}
#objc private func activate() {
NSRunningApplication.current.activate(options: .activateIgnoringOtherApps)
}
First, you should use -[NSApplication setActivationPolicy:] rather than TransformProcessType() and -[NSApplication setPresentationOptions:] rather than SetSystemUIMode() in modern code. If switching to those is not enough to fix the problem with the menu bar not updating, I recommend that you try using -setPresentationOptions: or +[NSMenu setMenuBarVisible:] to hide the menu bar and then immediately reverse that operation.
Also, drop that business with trying to activate the Dock.
So, something like:
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"LaunchAsAgentApp"]) {
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp setPresentationOptions:NSApplicationPresentationDefault]; // probably not necessary since it's the default
[NSMenu setMenuBarVisible:NO]; // these two lines may not be necessary, either; using -setActivationPolicy: instead of TransformProcessType() may be enough
[NSMenu setMenuBarVisible:YES];
}

XCode IBAction NSButton to show/hide separate image?

I have an IBAction that acts as a toggle switch for one of my functions; how can I make that same button show/hide a separate image depending on the isOn/isOff state?
Here's my code for the toggle function:
- (IBAction)toggleFunction:(id)sender;
{
if( isOn )
{
[self stopFunction: (NSButton *)sender];
isOn = NO;
}
else
{
[self startFunction: (NSButton *)sender];
isOff = YES;
}
}
You should use isOn instead isOff I think in Your else.
Try this:
- (IBAction)toggleFunction:(id)sender;
{
if( isOn == YES )
{
[self stopFunction: sender];
isOn = NO;
}
else
{
[self startFunction: sender];
isOn = YES;
}
}
** If this not working, say what's wrong. And also try NSLog Your functions to ensure that its called.

NSManagedObjectContext save causes NSTextField to lose focus

This is a really strange problem I'm seeing in my app. I have an NSTextField bound to an attribute of an NSManagedObject, but whenever the object is saved the textfield loses focus. I'm continuously updating the value of the binding, so this is far from ideal.
Has anyone seen anything like this before, and (hopefully) found a solution?
I encountered the issue recently and fixed it by changing the way the NSTextField was bound to the NSManagedObject attribute. Instead of binding the value of the text field to the selection.[attribute] key path of the NSArrayController, I bound the arrayController.selection.[attribute] keyPath of the view controller that had a proper outlet pointing to the controller.
For some reason, the NSTextField doesn't loose focus when the NSManagedObjectContext is saved if bound this way.
I want to share my solution. It will work for all fields without modification.
I have optimized it for this posting and removed some error checking, logging and thread safety.
- (BOOL)saveChanges:(NSError **)outError {
BOOL result = YES;
#try {
NSError *error = nil;
if ([self hasChanges]) {
// Get field editor
NSResponder *responder = [[NSApp keyWindow] firstResponder];
NSText *editor = [[NSApp keyWindow] fieldEditor: NO forObject: nil];
id editingObject = [editor delegate];
BOOL isEditing = (responder == editor);
NSRange range;
NSInteger editedRow, editedColumn;
// End editing to commit the last changes
if (isEditing) {
// Special case for tables
if ([editingObject isKindOfClass: [NSTableView class]]) {
editedRow = [editingObject editedRow];
editedColumn = [editingObject editedColumn];
}
range = [editor selectedRange];
[[NSApp keyWindow] endEditingFor: nil];
}
// The actual save operation
if (![self save: &error]) {
if (outError != nil)
*outError = error;
result = NO;
} else {
result = YES;
}
// Now restore the field editor, if any.
if (isEditing) {
[[NSApp keyWindow] makeFirstResponder: editingObject];
if ([editingObject isKindOfClass: [NSTableView class]])
[editingObject editColumn: editedColumn row: editedRow withEvent: nil select: NO];
[editor setSelectedRange: range];
}
}
} #catch (id exception) {
result = NO;
}
return result;
}
OK, so thanks to Martin for pointing out that I should read the docs a little more closely. This is expected behaviour, and here's what I did to get around it (use your judgement as to whether this is appropriate for you):
I save my context once every 3 seconds, checking at the start if the context has any changes before I bother executing the actual save: method on my NSManagedObjectContext. I added a simple incrementing/decrementing NSUInteger (_saveDisabler) to my Core Data controller class that is modified via the following methods:
- (void)enableSaves {
if (_saveDisabler > 0) {
_saveDisabler -= 1;
}
}
- (void)disableSaves {
_saveDisabler += 1;
}
Then all I do in my custom saveContext method is do a simple check at the top:
if (([moc hasChanges] == NO) || (_saveDisabler > 0)) {
return YES;
}
This prevents the save from occurring, and means that the focus is not stolen from any of my custom textfield subclasses. For completeness, I also subclassed NSTextField and enable/disable saves in my Core Data controller from the following methods:
- (void)textDidBeginEditing:(NSNotification *)notification;
- (void)textDidEndEditing:(NSNotification *)notification;
It might be a little messy, but it works for me. I'm keen to hear of cleaner/less convoluted methods if anyone has done this successfully in another way.

Resources