2014년 10월 9일 목요일

@ 매크로


1.
    NSString *string = @"<#string#>"

2.  
    id express = @(<#expression#>)

  (ex)
    NSInteger idx = 40;
    NSString *idxString = [@(idx) stringValue];

3.    
    NSArray *array =  @[<#objects, ...#>]

4.    
    @autoreleasepool {
        <#statements#>
    }

5.    
    char[] chars =  @encode(<#type-name#>)

6.    
    Protocol *protocol = @protocol(<#protocol-name#>)

7.    
    SEL sel = @selector(<#selector#>)

8.    
    @throw <#expression#>
    
9.
    NSDictonary *dic = @{<#key#>: <#object, ...#>}
    
10.
    @try {
        <#Code that can potentially throw an exception#>
    }
    @catch (NSException *exception) {
        <#Handle an exception thrown in the @try block#>
    }
    @finally {
        <#Code that gets executed whether or not an exception is thrown#>

    }

11. structure

    CGSize theSize = {
        .width = 100.f,
        .height = 100.f,
    };
    
    CGRect theRect = {
        {.x = 10.f, .y= 10.f},
        {.width = 200.f, .height = 200.f}
    };

2014년 10월 1일 수요일

UIAlertView & Block function

source

//
//  CustomUIAlertView.m
//  patternpie
//
//  Created by MYUNG GU KIM on 10/2/14.
//  Copyright (c) 2014 piestudio. All rights reserved.
//

#import "CustomUIAlertView.h"

@implementation CustomUIAlertView

- (void)showWithBlock:(void (^)(int selectedIndex))complete
{
    self.delegate = self;
    ptrComplete = complete;
    [self show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    ptrComplete((int)buttonIndex);
}


@end




header 

//
//  CustomUIAlertView.h
//  patternpie
//
//  Created by MYUNG GU KIM on 10/2/14.
//  Copyright (c) 2014 piestudio. All rights reserved.
//

#import <UIKit/UIKit.h>

void (^ptrComplete)(int);

@interface CustomUIAlertView : UIAlertView <UIAlertViewDelegate>
{
}
- ( void )showWithBlock: ( void ( ^ )( int selectedIndex) )complete;
@end





using


[[[CustomUIAlertView alloc] initWithTitle:nil message:@"Do you want Challenge?" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil] showWithBlock:^(int selectedIndex) {
            NSLog(@"You selected button %d", selectedIndex);
        }];




2014년 9월 30일 화요일

random


1.  0~10 까지의 랜덤 숫자

uint32_t random = arc4random_uniform(11);

2.  50~100 까지의 랜덤 숫자

uint32_t random = arc4random_uniform(51)+50;




3.  랜덤 숫자

unsigned int random = arc4random();


4. 컬러값 랜덤으로 가져오기

static func randomBackgroundColor() -> UIColor {
        let colors = [UIColor.rgb(5, 200, 123), UIColor.rgb(176, 158, 145), UIColor.rgb(106, 97, 248), UIColor.rgb(33, 173, 254), UIColor.rgb(249, 135, 40)]
        let randomIndex = Int(arc4random_uniform(UInt32(colors.count)))
        return colors[randomIndex]

    }

2014년 9월 23일 화요일

시, 분, 초 계산 방법


   CFTimeInterval _ticks;




    _ticks += 0.1;
    double seconds = fmod(_ticks, 60.0);
    double minutes = fmod(trunc(_ticks / 60.0), 60.0);
    double hours = trunc(_ticks / 3600.0);
    self.timerLabel.text = [NSString stringWithFormat:@"%02.0f:%02.0f:%04.1f", hours, minutes, seconds];
    

    NSLog(@"tick = %f text = %@", _ticks, self.timerLabel.text);





2014-09-24 02:59:49.241 TabbedBanner[961:60b] tick = 0.100000 text = 00:00:00.1
2014-09-24 02:59:49.341 TabbedBanner[961:60b] tick = 0.200000 text = 00:00:00.2
2014-09-24 02:59:49.441 TabbedBanner[961:60b] tick = 0.300000 text = 00:00:00.3
...
2014-09-24 02:59:53.141 TabbedBanner[961:60b] tick = 4.000000 text = 00:00:04.0
2014-09-24 02:59:53.241 TabbedBanner[961:60b] tick = 4.100000 text = 00:00:04.1
2014-09-24 02:59:53.341 TabbedBanner[961:60b] tick = 4.200000 text = 00:00:04.2
2014-09-24 02:59:53.441 TabbedBanner[961:60b] tick = 4.300000 text = 00:00:04.3
...
2014-09-24 03:03:35.186 TabbedBanner[961:60b] tick = 224.900000 text = 00:03:44.9
2014-09-24 03:03:35.286 TabbedBanner[961:60b] tick = 225.000000 text = 00:03:45.0
2014-09-24 03:03:35.386 TabbedBanner[961:60b] tick = 225.100000 text = 00:03:45.1
2014-09-24 03:03:35.486 TabbedBanner[961:60b] tick = 225.200000 text = 00:03:45.2
2014-09-24 03:03:35.586 TabbedBanner[961:60b] tick = 225.300000 text = 00:03:45.3
2014-09-24 03:03:35.686 TabbedBanner[961:60b] tick = 225.400000 text = 00:03:45.4

2014년 8월 13일 수요일

UITextField 의 text 가 영인지 체크하는 방법




- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSRange textFieldRange = NSMakeRange(0, [textField.text length]);
    if (NSEqualRanges(range, textFieldRange) && [string length] == 0) {
        [_keywordClearButton setHidden:YES];
    } else
        [_keywordClearButton setHidden:NO];
    
    return YES;

}

2014년 8월 5일 화요일

rotate 360 degree


1)

CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    fullRotation.fromValue = [NSNumber numberWithFloat:0];
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    fullRotation.duration = .3f;
    fullRotation.repeatCount = 1;

    [self.layer addAnimation:fullRotation forKey:@"360"];

2)

if(rotationCount == 3) {
        [UIView animateWithDuration:.4f delay:0.f usingSpringWithDamping:.5f initialSpringVelocity:0.f options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionBeginFromCurrentState animations:^{
            [_thumbnailImage setTransform:CGAffineTransformRotate(_thumbnailImage.transform, M_PI_2)];
        } completion:^(BOOL finished) {
            if (finished) {
                return;
            }
        }];
    }
    else {
        
        [UIView animateWithDuration:.2f animations:^{
            [_thumbnailImage setTransform:CGAffineTransformRotate(_thumbnailImage.transform, M_PI_2)];
        } completion:^(BOOL finished) {
            [self rotateAnimation];
        }];
        
    }
    
    rotationCount++;


3)

[UIView animateKeyframesWithDuration:.8f delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear|UIViewKeyframeAnimationOptionBeginFromCurrentState animations:^{
        
        // push the from- view to the back
        [UIView addKeyframeWithRelativeStartTime:0.0f relativeDuration:0.4f animations:^{
            [_thumbnailImage setTransform:CGAffineTransformRotate(_thumbnailImage.transform, M_PI_2)];
        }];
        [UIView addKeyframeWithRelativeStartTime:0.2f relativeDuration:0.4f animations:^{
            [_thumbnailImage setTransform:CGAffineTransformRotate(_thumbnailImage.transform, M_PI_2)];
        }];
        
        // slide the to- view upwards. In his original implementation Tope used a 'spring' animation, however
        // this does not work with keyframes, so we siulate it by overshooting the final location in
        // the first keyframe
        [UIView addKeyframeWithRelativeStartTime:0.6f relativeDuration:0.2f animations:^{
            [_thumbnailImage setTransform:CGAffineTransformRotate(_thumbnailImage.transform, M_PI_2)];
        }];
        [UIView addKeyframeWithRelativeStartTime:0.8f relativeDuration:0.2f animations:^{
            [_thumbnailImage setTransform:CGAffineTransformRotate(_thumbnailImage.transform, M_PI_2)];
        }];
        /*
        [UIView addKeyframeWithRelativeStartTime:1.f relativeDuration:0.4f animations:^{
            [_thumbnailImage setTransform:CGAffineTransformRotate(_thumbnailImage.transform, -(M_PI_2))];
        }];
        
        [UIView addKeyframeWithRelativeStartTime:.9f relativeDuration:0.2f animations:^{
            [_thumbnailImage setTransform:CGAffineTransformRotate(_thumbnailImage.transform, M_PI_4)];
        }];
        [UIView addKeyframeWithRelativeStartTime:1.4f relativeDuration:0.2f animations:^{
            [_thumbnailImage setTransform:CGAffineTransformRotate(_thumbnailImage.transform, -(M_PI_4))];
        }];
        [UIView addKeyframeWithRelativeStartTime:1.8f relativeDuration:0.2f animations:^{
            [_thumbnailImage setTransform:CGAffineTransformIdentity];
        }];*/
        
        
    } completion:^(BOOL finished) {
    }];

flip 360 degree


1) horizontally flip


_bodyView.transform = CGAffineTransformScale(_bodyView.transform, 1, -1);
    
    [UIView animateWithDuration:.5f delay:0 usingSpringWithDamping:.3f initialSpringVelocity:70.f options:UIViewAnimationOptionTransitionFlipFromTop animations:^{
        
        _bodyView.transform = CGAffineTransformScale(_bodyView.transform, 1, -1);
        
    } completion:^(BOOL finished) {

    }];