App Develop

[objective-c]UILabelを複製する

UILabelを複製する

-(UILabel*)getCloneUILabelByUILabel:(UILabel*)label
{
    NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:label];
    return [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
}
UILabel*cloneLabel = [self getCloneUILabelByUILabel:modelLabel];

[objective-c]2つの日付の差を日数で出す

2つの日付の差を日数で出す

int dayCount = [self getDaysCountByTwoDateString:@"2014-04-01" endDateString:@"2014-04-04"];
-(int)getDaysCountByTwoDateString:(NSString*)startDateString endDateString:(NSString*)endDateString
{
    NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
    [inputDateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *startInputDate = [inputDateFormatter dateFromString:startDateString];
    NSDate *endInputDate = [inputDateFormatter dateFromString:endDateString];
    
    float tmp= [endInputDate timeIntervalSinceDate:startInputDate];
    int day=(int)( tmp / (3600.0*24.0) );
    return day;
}

[objective-c]animateWithDurationを途中で止める

animateWithDurationを途中で止める

アニメーション実行

[UIView animateWithDuration:10.0f animations:^{
    targetView.frame=CGRectMalke(0.0,0.0,100.0,100.0);
} completion:^(BOOL finished) {
    
}];

アニメーション中止

[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:0.001 animations:^{
    targetView.alpha = 1.0;
}];

[objective-c]UIScrollViewのスクロール先のページインデックスを取得

pagingEnabled=YESのUIScrollViewのスクロール先のページインデックスを取得

ヘッダーファイル

<UIScrollViewDelegate>

メインファイル

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    CGFloat width = scrollView.frame.size.width;
    NSInteger page = (targetContentOffset->x + (0.5f * width)) / width;
    int index=(int)page;   
}

[objective-c]performSelectorの警告を消す

performSelectorの警告を消す

#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)

if (_delegate && _selector && [_delegate respondsToSelector:_selector]) {
    SuppressPerformSelectorLeakWarning([_delegate performSelector:_selector withObject:[NSNumber numberWithBool:success]]);
}

[objective-c]フォルダを作る

フォルダを作る

-(BOOL)makeDir:(NSString*)dirPath
{
    NSString* path=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    path=[path stringByAppendingPathComponent:dirPath];
    NSError *error;
    BOOL result = [[NSFileManager defaultManager] createDirectoryAtPath:path
                                            withIntermediateDirectories:YES
                                                             attributes:nil
                                                                  error:&error];
    return result;
}

[self makeDir:@"test1/test2/jack"];

上記を実行すると下記の3つのフォルダが作られる。
test1
test1/test2
test1/test2/jack