Showing posts with label iPad. Show all posts
Showing posts with label iPad. Show all posts

Wednesday, August 19, 2015

working with UIMenuController

working with UIMenuController

I am creating menu on longPressGesture of some view.
The code is as below.

- (void)showMenu:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        
        [self becomeFirstResponder];

        /*
         Set up th menu.
         */
        UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Item 1" action:@selector(item1Selected:)];
        UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Item 2" action:@selector(item2Selected:)];

        UIMenuController *menuController = [UIMenuController sharedMenuController];
        [menuController setMenuItems:@[menuItem1,menuItem2]];
        
        CGPoint location = [gestureRecognizer locationInView:[gestureRecognizer view]];
        CGRect menuLocation = CGRectMake(location.x, location.y, 0, 0);
        [menuController setTargetRect:menuLocation inView:[gestureRecognizer view]];
        
        [menuController setMenuVisible:YES animated:YES];
    }

}

- (void)item1Selected:(UIMenuController *)controller
{
    NSLog("Item 1 Selected");
}

- (void)item2Selected:(UIMenuController *)controller
{
    NSLog("Item 2 Selected");
}

// UIMenuController requires that we can become first responder or it won't display
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

Saturday, June 27, 2015

Get color from touch point

Get color from touch point

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint loc = [touch locationInView:self.view];
    self.pickedColor = [self colorOfPoint:loc];
    self.theColor.backgroundColor = self.pickedColor;
}

-(UIColor *) colorOfPoint:(CGPoint)point {
    unsigned char pixel[4] = {0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);

    CGContextTranslateCTM(context, -point.x, -point.y);

    [self.view.layer renderInContext:context];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0];

    return color;
}

Tuesday, June 16, 2015

Taking input from user via alert

Taking input from user via alert

UIAlertController * alert=   [UIAlertController alertControllerWithTitle:@"Text" message:@"Enter Text" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction * action) {
                                                   //Do Some action here
                                                   UITextField *textField = alert.textFields.firstObject;
                                                   NSLog(@"%@",textField.text);
                                               }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction * action) {
                                                       [alert dismissViewControllerAnimated:YES completion:nil];
                                                   }];
    
    [alert addAction:ok];
    [alert addAction:cancel];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
                        textField.placeholder = @"Enter text here";
    }];
    

    [self presentViewController:alert animated:YES completion:nil];

Monday, October 13, 2014

detecting iOS, iPhone, iPad and apple watch

detecting iOS, iPhone, iPad and apple watch

// detecting iOS


NSString *version = [[UIDevice currentDevice] systemVersion];
NSLog(@"Version:%@",version);

//detecting iPhone



add these line to your .h file

#define IS_IPHONE_4 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)480) < DBL_EPSILON)
#define IS_IPHONE_5 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON)
#define IS_IPHONE_6 (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)667) < DBL_EPSILON)
#define IS_IPHONE_6_PLUS (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)736) < DBL_EPSILON)


and use it in your .m file

if (IS_IPHONE_4)
        NSLog(@"iPhone 4");
else if (IS_IPHONE_5)
        NSLog(@"iPhone 5");

//detecting iPad


if ([UIDevice currentDevice].userInterfaceIdiom != UIUserInterfaceIdiomPad)
{
        //code for iPad
}

//detecting apple watch

add these line to your .h file

#define IS_WATCH_42 (fabs((double)self.contentFrame.size.height - (double)174) < DBL_EPSILON)

#define IS_WATCH_38 (fabs((double)self.contentFrame.size.height - (double)151) < DBL_EPSILON)


and use it in your .m file

if (IS_WATCH_42)
        NSLog(@"apple watch 42mm");
    else if (IS_WATCH_38)
        NSLog(@"apple watch 38mm");