Skip to content

Instantly share code, notes, and snippets.

@githubutilities
Last active August 30, 2015 05:30
Show Gist options
  • Select an option

  • Save githubutilities/76bfe8e027e387e0d6f3 to your computer and use it in GitHub Desktop.

Select an option

Save githubutilities/76bfe8e027e387e0d6f3 to your computer and use it in GitHub Desktop.
iOS Development Log

iOS 6 cookbook

Pass through events

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return nil;
}

Transparent view

Set backgroundColor of the subclass of UIView to [UIColor clearColor]. You should set it in the designated initializer, because backgroundColor is set before drawRect: of UIView.

self.backgroundColor = [UIColor clearColor];

View memory management

The “problem” is that iOS is very finicky about not wasting memory

and since the modal view will completely cover the one beneath it,

it doesn’t make much sense to keep it loaded.

Therefore, iOS unloads the view that presents the modal one.

You may check this behavior by implementing -viewWillDisappear: and -viewDidDisappear:.

Drawing graph that is rotated around a specific point

CGAffineTransform transform = CGAffineTransformMake(cos(a),sin(a),-sin(a),cos(a),x-x*cos(a)+y*sin(a),y-x*sin(a)-y*cos(a));

Drawing text using CGContextShowTextAtPoint

Although both of the API support drawing in a Core Graphics context, CGContextShowTextAtPoint method may not support chinese language, while NSString's drawAtPoint: support more encodings like chinese.

Use Core Graphics to Draw

Tricks for debugging variables value

If value of the a variable doesn't go right, do the following:

  • try to make it as a property and overwrite its setter
  • NSLog its value in setter
  • Insert a breakpoint in the setter
  • Run, everytime it stops at the breakpoint, watch the call stack of the main thread.

Difference between frame and bounds and center

frame and center use parent coordinate system, while bounds use coodinate system relative to its own.

NSString micro syntactic sugar

#define WORD_EXTENSION  @"doc docx "
#define PPT_EXTENSION   @"ppt pptx "
#define EXCEL_EXTENSION @"xls xlsx "
#define DOC_EXTENSION WORD_EXTENSION PPT_EXTENSION EXCEL_EXTENSION

NSArray *extensions = [DOC_EXTENSION componentsSeparatedByString:@" "];

for (NSString *extension in extensions) {
    NSLog(@"%@", extension);
}

Updating frame of UIView

frame of UIView is relative to its parent UIView. If you need to update its frame, please copy it into a CGRect and assign it back to its frame. Don't try to modify it by view.frame.origin = CGPointZero; which won't work.

CGRect newFrame = view.frame;
newFrame.origin = CGPointZero;
view.frame = newFrame;

Clear Xcode caches

remove ~/Library/Developer/Xcode/DerivedData and /var/folders.

Debugging UIView

After paused at breakpoint, in lldb, use the following command

po [self.window recursiveDescription]

Finding target-actions for UIButton

UIButton implements UIControl to handle target-actions. So the following snippet makes use of APIs of UIControl.

    UIView *superView = self.window.rootViewController.view;
    for (UIView *view in superView.subviews) {
        if ([[view class] isSubclassOfClass:[UIButton class]]) {
            // suppress warning using pointer casting
            UIButton *btn = (UIButton *)view;
            
            // logging out the targets of UIButton
            NSSet *targets = [btn allTargets];
            NSLog(@"targets of UIButton(%@)", btn);
            for (id target in targets) {
                NSLog(@"target %@", [target class]);
                
                // logging out actions for specific target and UIControlEventTouchUpInside
                NSArray *actions = [btn actionsForTarget:target forControlEvent:UIControlEventTouchUpInside];
                for (NSString *action in actions) {
                    NSLog(@"action %@", action);
                }
            }
            
            // logging out all control events
            NSLog(@"%lu", (unsigned long)[btn allControlEvents]);
        }
    }

Reveal app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment