Friday, 22 January 2016

UITableview tapping on UIImageView

#import <objc/runtime.h> // for objc_setAssociatedObject / objc_getAssociatedObject


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView cellForItemAtIndexPath:indexPath];

    UIImageView* iv = cell.contentView.subviews.lastObject;

    UIImageView* ivExpand = [[UIImageView alloc] initWithImage: iv.image];
    ivExpand.contentMode = iv.contentMode;
    ivExpand.frame = [self.view convertRect: iv.frame fromView: iv.superview];
    ivExpand.userInteractionEnabled = YES;
    ivExpand.clipsToBounds = YES;

    objc_setAssociatedObject( ivExpand,
                              "original_frame",
                              [NSValue valueWithCGRect: ivExpand.frame],
                              OBJC_ASSOCIATION_RETAIN);

    [UIView transitionWithView: self.view
                      duration: 1.0
                       options: UIViewAnimationOptionAllowAnimatedContent
                    animations:^{

                        [self.view addSubview: ivExpand];
                        ivExpand.frame = self.view.bounds;

                    } completion:^(BOOL finished) {

                        UITapGestureRecognizer* tgr = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector( onTap: )];
                        [ivExpand addGestureRecognizer: tgr];
                    }];
}

- (void) onTap: (UITapGestureRecognizer*) tgr
{
    [UIView animateWithDuration: 1.0
                     animations:^{

                         tgr.view.frame = [objc_getAssociatedObject( tgr.view,
                                                                    "original_frame" ) CGRectValue];
                     } completion:^(BOOL finished) {

                         [tgr.view removeFromSuperview];
                     }];
}

UIImageView to display fullscreen by tapping using animation.

  @interface viewController : UIViewController <UIGestureRecognizerDelegate>{
    UITapGestureRecognizer *tap;
    BOOL isFullScreen;
    CGRect prevFrame;
}

- (void)viewDidLoad {

    [super viewDidLoad];
isFullScreen = false;
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen)];
tap.delegate = self;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
    BOOL shouldReceiveTouch = YES;

    if (gestureRecognizer == tap) {
        shouldReceiveTouch = (touch.view == yourImageView);
    }
    return shouldReceiveTouch;
}

-(void)imgToFullScreen{
    if (!isFullScreen) {
        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
            //save previous frame
            prevFrame = yourImageView.frame;
            [yourImageView setFrame:[[UIScreen mainScreen] bounds]];
        }completion:^(BOOL finished){
            isFullScreen = true;
        }];
        return;
    } else {
        [UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
            [yourImageView setFrame:prevFrame];
        }completion:^(BOOL finished){
            isFullScreen = false;
        }];
        return;
    }
}

How to remove specify UILabel subview from UIButton?


//[UIButton addSubview:UILabel];

for(int i = 0 ; i < button.subviews.count ; i++)
{
   UIView *v = [button.subviews objectAtIndex:i];
   if([v isMemberOfClass:[UILabel class]])
   {
      [v removeFromSuperview];
      i--;
   }
}

Thursday, 21 January 2016

Foreground notification

  [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(allFunctions)
                                                 name:UIApplicationWillEnterForegroundNotification

                                               object:nil];


-(void) allFunctions{ //call any functions that need to be run when application will enter foreground
    NSLog(@"calling all functions...application just came back from foreground");
}

Monday, 11 January 2016

UITableView - Reload table view with smooth animation

dispatch_async(dispatch_get_main_queue(), ^{
        [UIView transitionWithView:<"TableName">
                          duration:0.1f
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^(void) {
                            [<"TableName"> reloadData];
                        } completion:NULL];       
    });

Friday, 8 January 2016

enum

.h file

enum schedualePage
{
    SCHEDULE,
    VIEW_SCHEDULE

};

.m file

Create enum object

enum schedualePage scheduleObj;

switch case:

switch (scheduleObj) {
            case SCHEDULE:
               NSLog(@"SCHEDULE case");
                break;
            case VIEW_SCHEDULE:
               NSLog(@"VIEW_SCHEDULE case");
                break;
            default:
                break;
        }

set 
scheduleObj = SCHEDULE;

Wednesday, 6 January 2016

Background thread

    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // Add code here to do background processing

            dispatch_async( dispatch_get_main_queue(), ^{
                // Add code here to update the UI/send notifications based on the
                // results of the background processing
            });

        });

(OR)

    dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
            // Add code here to do background processing

            dispatch_async( dispatch_get_main_queue(), ^(void){
                // Add code here to update the UI/send notifications based on the
                // results of the background processing
            });
        });