欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wpp项目的一些记录

程序员文章站 2022-07-12 13:22:36
...

1.如果viewController都是通过push和pop来管理的,那rootViewController相当是个单例了,可以考虑写成单例

2.一个button通过addtarget添加响应事件后,响应的函数参数(id)sender就是那个view,通过view的subviews和super可以得到相应的父view和子view进行view的管理,而不需要去写成成员变量进行管理

3.删除view时,先进行removeFromSuperview,在release和置为nil

4.button的相应事件一般定义在点击时(UIControlEventTouchDown)和点击后UIControlEventTouchUpInside,第一个常用在点击时ui上的变化,第二个常用于点击后按钮响应的事件

5.view在设置背景图时,为了高清图自适应view大小,可以选择在view上add一个UIImageView,在设置view的setBackImageView

6.view subviews得到所有子view,通过view isKindOfClass:[xxx class] 判断view的类型,如此获取相应的view,当然也可通过view的大小或者其他属性判断

7.当你需要为当前的响应添加一个view时,并且需要提醒的view不受其他view的push或者pop的影响,可以选择把view通过[UIApplication shaApplication].keyWindow addSubview:xxx 添加到window上

8.有时候创建好的UIActivityIndicatorView想立即弹出显示,可以选择将主线程挂起0.00001s,通过performSelector 中的afterDelay:0.00001阻塞主线程,马上让出cpu时间让菊花弹出

9.label setFont:[UIFont boldSystemFontOfSize(加粗):18];非加粗systemFontOfSize

10.让输入框第一个输入位置不紧挨着左边:

    {
        UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 19)];
        self.inputField.leftView = paddingView;
        [paddingView release];
        self.inputField.leftViewMode = UITextFieldViewModeAlways;
    }
UITextFiled常用设置:

    NSString *defaultStr = getMulNSStr([NSString stringWithFormat:@"SHARE_PLAY_CODE"],localFileName);
    [self.inputField setPlaceholder:defaultStr];
    if (!isPhone())
    {
        [self.inputField becomeFirstResponder];
    }
    self.inputField.keyboardType = UIKeyboardTypePhonePad;//UIKeyboardTypeNumbersAndPunctuation;
    self.inputField.contentVerticalAlignment = (UIControlContentVerticalAlignment)UIControlContentHorizontalAlignmentCenter;
//    self.inputField.secureTextEntry = YES;
    [self.inputField setEnablesReturnKeyAutomatically:YES];
    [self.inputField setReturnKeyType:UIReturnKeyDone];
    [self.inputField setDelegate:(id)self];
    [self.inputField addTarget:self action:@selector(accessCodeTextFiledDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.bgView addSubview:self.inputField];
11.添加gif动画:

- (void) addProgressBarGif
{
    NSData *progressGifData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:PUBLIC_PROGRESS_BAR_GIF_IPAD3 ofType:@"gif"]];
    UIWebView *gifWebView = [[[UIWebView alloc] initWithFrame:CGRectMake((62+14)/2, 424/4-30/4.f, 524/2, 30/2)] autorelease];
    gifWebView.userInteractionEnabled = NO;
    [gifWebView loadData:progressGifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    [self.bgView addSubview:gifWebView];
}

12.keyboard的显示和隐藏事件:

- (void)addKeyboardEvents
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)removeKeyboardEvents
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    if (bgButton == nil)
    {
        self.bgButton = [[[UIButton alloc] initWithFrame:self.bounds] autorelease];
        [self.bgButton setBackgroundColor:[UIColor clearColor]];
        [self.bgButton addTarget:self action:@selector(keyboardWillHide:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.bgButton];
        [self bringSubviewToFront:self.bgView];
    }
    else
    {
        [self.bgButton setEnabled:YES];
    }

    if (self.keyBoardInfo == nil)
    {
        self.keyBoardInfo = [notification userInfo];
    }
    
    NSValue* aValue = [self.keyBoardInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self convertRect:keyboardRect fromView:nil];
    
    CGFloat keyboardTop = keyboardRect.size.height;
    CGRect newTextViewFrame = self.bgView.frame;
    if (isPhone())
    {
        if (self.bgView.frame.size.height == 424/2)
        {
            newTextViewFrame.origin.y = - self.bgView.frame.origin.y + 20;
        }
        else
        {
            newTextViewFrame.origin.y = - self.bgView.frame.origin.y - 40;
        }
    }
    else
    {
        newTextViewFrame.origin.y = keyboardTop - self.bgView.frame.origin.y;
    }
    
    NSValue *animationDurationValue = [self.keyBoardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    
    self.bgView.frame = newTextViewFrame;
    
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    if ([self.bgButton isEnabled])
    {
        [self.bgButton setEnabled:NO];
        [self.inputField resignFirstResponder];
    }
//    NSDictionary* userInfo = [notification userInfo];
    if (self.keyBoardInfo == nil)
    {
        self.keyBoardInfo = [notification userInfo];
    }
    
    NSValue *animationDurationValue = [self.keyBoardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    
    if (isPhone())
    {
        [self.bgView setFrame:CGRectMake(self.bgView.frame.origin.x, IPHONE_ITOUCH_4_4S_HEIGHT/2 - self.bgView.frame.size.height/2, self.bgView.frame.size.width, self.bgView.frame.size.height)];
    }
    else
    {
        [self.bgView setFrame:CGRectMake(self.bgView.frame.origin.x, IPAD_2_3_HEIGHT/2 - self.bgView.frame.size.height/2, self.bgView.frame.size.width, self.bgView.frame.size.height)];
    }
    
    [UIView commitAnimations];
}

12.textFiled常用的delegate要清楚:

- (BOOL)textFieldShouldReturn:(UITextField *)textField

- (void)textFieldDidBeginEditing:(UITextField *)textField

13.设置button上的文字:

self.closeButton.titleLabel.adjustsFontSizeToFitWidth = YES;
    self.closeButton.titleLabel.text = getMulNSStr([NSString stringWithFormat:@"PREVIEW_CLOSEBTN"],localFileName);
    if (isPhone())
    {
        [self.closeButton.titleLabel setFont:[UIFont boldSystemFontOfSize:12.f]];
    }
[self.closeButton setTitleColor:[UIColor colorWithRed:115.f/255.f green:84.f/255.f blue:17.f/255.f alpha:1.0f] forState:UIControlStateNormal];
14.layer可通过zPosition设置上下层位置,view通过sendSubviewToBack和bringSubviewToFront设置上下层view的关系

15.当前view只支持横屏

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft ||
    [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight;
}
16.label的属性,label中的文字填满后自动填充 ... 的属性:
titleName = [titleName stringByDeletingPathExtension];

17.用通知解耦合:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshSlide:) name:kNotificationRefreshSlide object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self name:kNotificationRefreshSlide object:nil];


使用时请仔细揣摩一对多,多对一的关系

18.使用ios的手势时,千万记得在手势的响应事件中添加当前手势的状态判断,否则这个函数后被调用很多遍:

if (recognizer.state == UIGestureRecognizerStateEnded)

19.用调用delegate的方法时,请先判断方法是否能到达,增强代码的健壮性:

if ([self.refreshSlideAndCellDelegate respondsToSelector:@selector(gotoSelectThumbnail:beforIndex:)])
        {
            [self.refreshSlideAndCellDelegate gotoSelectThumbnail:prevSlide beforIndex:index];
        }
20.使用row创建NSIndexPath时,你是不是常常没有加inSection,这是个隐患:

NSIndexPath *nextSlide = [NSIndexPath indexPathForRow:indexinSection:0];
21.scrollview一般的初始化:

- (void)initSlideScrollView
{
    slideScrollView = [[[UIScrollView alloc] initWithFrame:self.view.bounds] autorelease];
    slideScrollView.contentSize = slideImageView.frame.size;
    m_zoneSize = slideScrollView.contentSize;
    [slideScrollView addSubview:slideImageView];
    
    [slideScrollView setMinimumZoomScale:1.0];
    [slideScrollView setMaximumZoomScale:2.5];
    [slideScrollView setBounces:NO];
    slideScrollView.bouncesZoom = NO;
    slideScrollView.decelerationRate = 0.1f;
    
    [slideScrollView setDelegate:self];
    [slideScrollView setShowsVerticalScrollIndicator:NO];
    [slideScrollView setShowsHorizontalScrollIndicator:NO];
    [self.view addSubview:slideScrollView];
}
22.当你决定手动调用drawRect方法时,那你就应该disable掉自动调用这种隐患:

-(void)drawRect:(CGRect)rect

{
if (isNeedDrawRect)

{

.....
}
}

-(void)needDrawRect:(CGRect)rect

{
isNeedDrawRect = YES;

[self drawRect:rect];

isNeedDrawRect = NO;

}

23.initWithCGImage要小心啊,这玩意去创建UImage,真不保险

24.线程的等待和唤醒功能,在同步中实用:

- (void)conditionWait:(NSCondition*)threadCondition
{
    [threadCondition lock];
    [threadCondition wait];
    [threadCondition unlock];
}

- (void)conditionSignal:(NSCondition*)threadCondition
{
    [threadCondition lock];
    [threadCondition signal];
    [threadCondition unlock];
}

25.简单的动画用view实现也不错:

- (void)zoomImageView:(UIImageView*)imageView mark:(NSString*)markAnim
{
    [UIView beginAnimations:markAnim context:nil];
    
    [UIView setAnimationDuration:0.2];
    
    [UIView setAnimationDelegate:self];

    CGAffineTransform newTransform =  CGAffineTransformScale(imageView.transform, _minimumPageScale, _minimumPageScale);
    [imageView setTransform:newTransform];
    
    [UIView commitAnimations];
}
动画结束后执行的,有这个delegate:

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context


动画都是非阻塞的,动画时应该屏蔽屏幕的点击事件:

self.view.userInteractionEnabled = NO;

26.文件大小,创建者等信息:

NSString *fileMsg = nil;
    NSError* error;
    NSDictionary* fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
    if (fileAttributes != nil)
    {
        NSNumber *fileSize;
        NSString *fileOwner, *creationDate, *filesizeStr;
        NSDate *fileModDate;
        
        //文件大小
        fileSize = [fileAttributes objectForKey:NSFileSize];
        double size = [fileSize doubleValue];

        NSString* strFileInfoFormat = nil;
        if (size <= 0) {
            filesizeStr = [NSString stringWithFormat : @""];
        }
        if(size < 1024)
        {
            strFileInfoFormat = @"%0.0f B";
        }
        else
        {
            size /= 1024.f;
            if (size < 1024)
            {
                strFileInfoFormat = @"%0.1f KB";
            }
            else
            {
                size /= 1024.f;
                if (size < 1024)
                    strFileInfoFormat = @"%0.1f MB";
                else
                {
                    size /= 1024.f;
                    strFileInfoFormat = @"%0.1f GB";
                }
            }
            
        }
        filesizeStr = [NSString stringWithFormat : strFileInfoFormat, size];
        
        //文件创建日期
        creationDate = [NSString stringWithFormat:@"%@",[fileAttributes objectForKey:NSFileCreationDate]];

        //文件所有者
        fileOwner = [NSString stringWithFormat:@"%@",[fileAttributes objectForKey:NSFileOwnerAccountName]];
        
        //文件修改日期
        fileModDate = [fileAttributes objectForKey:NSFileModificationDate];
        
        NSTimeZone *zone = [NSTimeZone systemTimeZone];
        NSInteger interval = [zone secondsFromGMTForDate:fileModDate];
        fileModDate = [fileModDate dateByAddingTimeInterval:interval];
        
        NSString *fileData = [NSString stringWithFormat:@"%@", fileModDate];
        fileData = [fileData substringToIndex:16];
        
        NSString *wordDate = getMulNSStr([NSString stringWithFormat:@"PHOTOWALL_LAST_MODIFIED"],localFileName);
        
        fileMsg = [NSString stringWithFormat:@"%@   %@ %@", filesizeStr, wordDate, fileData];
    }
27.代码强耦合,设计模式是软肋啊。。。