Skip to content

Instantly share code, notes, and snippets.

@ClassicThunder
Last active August 29, 2015 14:19
Show Gist options
  • Save ClassicThunder/b14aa1f53857a74dadb9 to your computer and use it in GitHub Desktop.
Save ClassicThunder/b14aa1f53857a74dadb9 to your computer and use it in GitHub Desktop.
Objective-c reference.

###Generating a screenshot

- (UIImage *)screenshot
{
    CGSize imageSize = CGSizeZero;
    
    imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, window.center.x, window.center.y);
        CGContextConcatCTM(context, window.transform);
        CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
        
        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
        {
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        }
        else
        {
            [window.layer renderInContext:context];
        }
        CGContextRestoreGState(context);
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

###Keyboard

######Subscribe to opening and closing

  //Subscribe to the Keyboard events
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(keyboardFrameDidChange:)
                                               name:UIKeyboardWillChangeFrameNotification
                                             object:nil];
                                             
  - (void)keyboardFrameDidChange:(NSNotification *)notification

######Notification info

  CGRect keyboardBeginFrame = [[notification userInfo][UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  CGRect keyboardFrameBegin = [self.view convertRect:keyboardBeginFrame toView:nil];
    
  CGRect keyboardEndFrame = [[notification userInfo][UIKeyboardFrameEndUserInfoKey] CGRectValue];
  CGRect keyboardFrameEnd = [self.view convertRect:keyboardEndFrame toView:nil];
  
  if (keyboardFrameBegin.origin.y > keyboardFrameEnd.origin.y) //Indicates it is opening
  
  //Animation lenght 
  [[notification userInfo][UIKeyboardAnimationDurationUserInfoKey] doubleValue];

######Get the KeyBoard window

- (UIWindow*)findKeyboardWindow
{
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        if ([NSStringFromClass([window class]) isEqualToString:@"UITextEffectsWindow"])
        {
            return window;
        }
    }
    return nil;
}

###UISlider

######Snapping to increments.

  [self addTarget:self
           action:@selector(valueChanged:)
 forControlEvents:UIControlEventValueChanged];

  
  - (void)valueChanged:(id)sender
  {
      self.value = roundf(self.value / X) * X;
  }

###UIView

######Using layers to add borders to the top and bottom edge of a view.

Border along the top

  CALayer *topBorder = [CALayer layer];
  topBorder.backgroundColor = [[UIColor lightGrayColor] CGColor];
  topBorder.frame = CGRectMake(0, 0, self.frame.size.width, 1);
  [self.layer addSublayer:topBorder];

Border along the bottom

  CALayer *bottomBorder = [CALayer layer];
  bottomBorder.backgroundColor = [[UIColor lightGrayColor] CGColor];
  bottomBorder.frame = CGRectMake(0, self.frame.size.height - 1, self.frame.size.width, 1);
  [self.layer addSublayer:bottomBorder];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment