2014년 2월 26일 수요일

hexa 값으로 컬러값 한번에 설정


RGB hexa 값(0xfffefa)으로 RGB값 설정 예제)

    unsigned rgbValue = 0xfffefa;
    UIColor *bgColor = [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0
                                       green:((rgbValue & 0xFF00) >> 8)/255.0
                                        blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
    [myView setBackgroundColor:bgColor];

2014년 2월 21일 금요일

Animation 2


방법 1)

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                           forView:newView
                             cache:YES];
    [containerView setHidden:NO];
    [containerView addSubview:newView];
    [UIView commitAnimations];


방법 2)
    [UIView transitionWithView: containerView duration:0.5
                       options:UIViewAnimationOptionCurveEaseInOut 
                    animations:^ {
                        [containerView addSubview:newView];
                    } completion:nil];



방법 3)
CATransition *animation = [CATransition animation];
[animation setDuration:0.2];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromRight];
    [newView.layer addAnimation:animation forKey:nil];
    [containerView addSubview:newView];

remove subview

CATransition *animation = [CATransition animation];
            [animation setDuration:0.5];
            [animation setType:kCATransitionReveal];
            [animation setSubtype:kCATransitionFromLeft];
            [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
            [[_refMenuContainerView layer] addAnimation:animation forKey:@"SlideView"]; //@"SwitchToNewView" , kCATransition ,,,,
            

            [subview removeFromSuperview];

2014년 2월 16일 일요일

UINavigationController 에서 뒤로(BACK) 두번 하기 (Pop)

popToViewController를 사용 하면 됨.



int viewControllerStackCount = self.navigationController.viewControllers.count;

    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:(viewControllerStackCount-3)] animated:YES];

2014년 2월 14일 금요일

Animation

1. UINavigationController에 present & dismiss animation 적용

////// presentedViewController animation

CATransition* transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionMoveIn;
    transition.subtype = kCATransitionFromTop;
    [self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
    
    BoolshelfSubViewController *controller= [self.storyboard instantiateViewControllerWithIdentifier:@"BoolshelfSubViewController"];

    [self.navigationController pushViewController:controller animated:NO];
   

////// dismissViewControllerAnimated animation

CATransition* transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionReveal;
    transition.subtype = kCATransitionFromBottom;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];

    [[self navigationController] popViewControllerAnimated:NO];


2.  UIView를 이용한 animation 적용

[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
[UIView setAnimationDuration:0.7];

[self.navigationController pushViewController:pageViewController animated:NO];
pageViewController = nil;
    
[UIView commitAnimations];


3. UIView Layer에 animation 적용 


CATransition *animation = [CATransition animation];
[animation setDuration:0.2];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromRight];
    [menuView.layer addAnimation:animation forKey:nil];

    [_menuContainerView addSubview:menuView];

2014년 1월 21일 화요일

sns 공유 (Twitter, Facebook, Line)

+ (void)postToTwitter:(UIViewController *)originController  withData:(NSDictionary *)dic {
    SLComposeViewController *tweetSheet = [SLComposeViewController
                                           composeViewControllerForServiceType:
                                           SLServiceTypeTwitter];
    tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
        switch(result) {
                //  This means the user cancelled without sending the Tweet
            case SLComposeViewControllerResultCancelled:
                break;
                //  This means the user hit 'Send'
            case SLComposeViewControllerResultDone:
                break;
        }
    };
    
    //  Set the initial body of the Tweet
    [tweetSheet setInitialText:[dic valueForKey:@"text"]];
    NSString *imageUrl = [dic valueForKey:@"imageurl"];
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if(![tweetSheet addImage:[UIImage imageWithData:data]]) {
            NSLog(@"Unable to add the image!");
        } else {
            if (![tweetSheet addURL:[NSURL URLWithString:[dic valueForKey:@"referenceurl"]]]){
                NSLog(@"Unable to add the URL!");
            }
            [originController presentViewController:tweetSheet animated:YES completion:^{
                NSLog(@"Tweet sheet has been presented.");
            }];
        }
    }];
}

+ (void)postToFacebook:(UIViewController *)originController withData:(NSDictionary *)dic {
    SLComposeViewController *facebookSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    
    [facebookSheet setInitialText:[dic valueForKey:@"text"]];
    [facebookSheet addURL:[NSURL URLWithString:[dic valueForKey:@"referenceurl"]]];
    NSString *imageUrl = [dic valueForKey:@"imageurl"];
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        [facebookSheet addImage:[UIImage imageWithData:data]];
    }];
    
    [originController presentViewController:facebookSheet animated:YES completion:Nil];
}

+ (void)postToLine:(UIViewController *)originController withData:(NSDictionary *)dic {
    //텍스트 전송 링크 전송
    NSString *_resultString = [[NSString stringWithFormat:@"%@%@",[dic valueForKey:@"text"],[dic valueForKey:@"imageurl"]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *LineUrlString = [NSString stringWithFormat:@"line://msg/text/%@",_resultString];
    BOOL r =[[UIApplication sharedApplication] openURL:[NSURL URLWithString:LineUrlString]];
    if (!r) {
        [[[UIAlertView alloc] initWithTitle:@"" message:@"LINEがインストールされていません。" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil,nil] show];
    }

}

이미지 리사이징(이율 유지)

//이미지 비율을 유지하면서 최대 가로 or 높이보다 작은 이미지 사이즈 생성
+ (UIImage *)getScaledImage:(UIImage *)image scaledToMaxWidth:(CGFloat)width maxHeight:(CGFloat)height {
    CGFloat oldWidth = image.size.width;
    CGFloat oldHeight = image.size.height;
    
    if (oldWidth < width && oldHeight < height)
        return image;
    
    CGFloat scaleFactorW =1;
    CGFloat scaleFactorH =1;
    
    if (oldWidth > width)
        scaleFactorW = width / oldWidth;
    if(oldHeight > height)
        scaleFactorH = height / oldHeight;
    
    CGFloat scaleFactor = (scaleFactorW<scaleFactorH)?scaleFactorW:scaleFactorH;
    
    
    CGFloat newHeight = oldHeight * scaleFactor;
    //CGFloat newWidth = oldWidth * scaleFactor;
    CGSize newSize = CGSizeMake(width, newHeight);
    
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

//이미지 비율을 유지하면서 최대 가로 보다 작은 이미지 사이즈 생성
+ (UIImage *)getScaledImage:(UIImage *)image scaledToMaxWidth:(CGFloat)width {
    CGFloat oldWidth = image.size.width;
    CGFloat oldHeight = image.size.height;
    
    CGFloat scaleFactor=1;
    
    if (oldWidth > width) {
        scaleFactor = width / oldWidth;
    } else  //oldWidth<width and height==0이면, scale하지 않음.
        return image;
    
    CGFloat newHeight = oldHeight * scaleFactor;
    CGFloat newWidth = oldWidth * scaleFactor;
    CGSize newSize = CGSizeMake(newWidth, newHeight);
    
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;

}

2014년 1월 15일 수요일

이미지 패턴으로 컬러값 지정

[UIColor colorWithPatternImage:[UIImage imageNamed:@"이미지 이름"];


example)
//배경을 bh_sand.png 파일로 설정 하기
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_sand"]];