zoukankan      html  css  js  c++  java
  • IOS开发基础知识--碎片17

    1:contentSize、contentInset和contentOffset区别

    contentSize 是scrollview中的一个属性,它代表scrollview中的可显示区域,假如有一个scrollview,它的frame为(0,0,320,480),而它的contentSize为(320,960).也就是说,这个scrollview整个内容的大小为(320,960),要通过上下滑动scrollview来查看(320,480)后的内容。
    
    contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是(0 ,-480),也就是y偏移了480
    
    contentInset 是scrollview中contentView.frame.origin与scrollview.frame.origin的关系,比如contentView的frame为(0,30,320,480),那么contentInset则为(0, 30),它也可以设置上下左右

    2:IOS虚拟器安装其它Simulator

    下载后的dmg安装.这里主要以iOS7.0模拟器的离线安装为例进行说明,其他版本以此类推:
    
    下载ios_7_0_simulator.dmg后打开dmg文件,可以看到安装包iPhoneSimulatorSDK7_0.pkg,使用安装器安装此安装包,默认会安装在所选分区的/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk目录下,完全退出Xcode后将刚才安装的iPhoneSimulator7.0.sdk整个目录复制或移动到/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs目录下即可,(Xcode.app右键可以"显示包内容“)重新启动Xcode一般就可以使用相应版本的模拟器进行开发和调试了。
    
    离线安装还有一个简单的办法就是将以前安装过的旧版本的Xcode如Xcode5.0.2下面已经安装好了的iOS模拟器直接复制过来使用,目录位置都一样,都是在Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs里面。这样就不用再下载离线安装包了。

    3:输入框中的inputaccessoryview和inputview

    UITextFields和UITextView有一个inputAccessoryView的属性,当你想在键盘上展示一个自定义的view时,你就可以设置该属性。你设置的view就会自动和键盘keyboard一起显示了。需要注意的是,你所自定义的view既不应该处在其他的视图层里,也不应该成为其他视图的子视图。其实也就是说,你所自定义的view只需要赋给属性inputAccessoryView就可以了,不要再做其他多余的操作。
    
    inputview则是键盘视图,当其为nil时则弹出的是系统默认的键盘;
    
    实例一(给键盘上方设置一个工具条的方式):
    
    - (void)createKeyboardTool
    {
        keyboardTool = [[UIToolbar alloc] initWithFrame: CGRectMake(kZero, kZero, kScreenW, 44.0f)];
        NSMutableArray *myToolBarItems = [NSMutableArray array];
    
      //创建键盘工具条上面的按钮,并设置点击事件
        UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(cancelAction)];
        UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(saveAction)];
        UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(saveAction)];
        
        [myToolBarItems addObject:cancelBtn];
        [myToolBarItems addObject:space];
        [myToolBarItems addObject:saveBtn];
        keyboardTool.items = myToolBarItems;
    }
    
    //inputAccessoryView:设置键盘顶部显示的工具条;inputView:自定义键盘
        commentTextView = [[UITextView alloc]initWithFrame:CGRectMake(kZero, kZero, kScreenW, 200)];
        [commentTextView becomeFirstResponder];
        commentTextView.inputAccessoryView = keyboardTool;
    
    
    实例二(修改键盘视图,进行切换自定义视图跟系统自带视图):
    
    
    /**
     *  切换键盘
     */
    - (void)switchKeyboard
    {
        // self.textView.inputView == nil : 使用的是系统自带的键盘
        if (self.textView.inputView == nil) {
            // 切换为自定义的表情键盘 emtionKeyboard为一个视图
            self.textView.inputView = self.emotionKeyboard;
            // 显示键盘按钮
            self.toolbar.showKeyboardButton = YES;
        } else { 
            // 切换为系统自带的键盘
            self.textView.inputView = nil;
            // 显示表情按钮
            self.toolbar.showKeyboardButton = NO;
        }
        // 开始切换键盘 这个是为固定用的
        self.switchingKeybaord = YES;    
        // 退出键盘
        [self.textView endEditing:YES];    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 弹出键盘,让其慢点实现 
            [self.textView becomeFirstResponder];        
            // 结束切换键盘
            self.switchingKeybaord = NO;
        });
    }
    
    
    /**
     * 键盘的frame发生改变时调用(显示、隐藏等)
     */
    - (void)keyboardWillChangeFrame:(NSNotification *)notification
    {
        // 如果正在切换键盘,就不要执行后面的代码
        if (self.switchingKeybaord) return;
        
        NSDictionary *userInfo = notification.userInfo;
        // 动画的持续时间
        double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        // 键盘的frame
        CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        
        // 执行动画
        [UIView animateWithDuration:duration animations:^{
            // 工具条的Y值 == 键盘的Y值 - 工具条的高度
            if (keyboardF.origin.y > self.view.height) { // 键盘的Y值已经远远超过了控制器view的高度
                self.toolbar.y = self.view.height - self.toolbar.height;
            } else {
                self.toolbar.y = keyboardF.origin.y - self.toolbar.height;
            }
        }];
    }

    4:修改UISearchBar中关于cannel取消的文字

    -(UISearchBar *)mySearchBar
    {
        if (_mySearchBar==nil) {
            _mySearchBar=[[UISearchBar alloc]init];
            _mySearchBar.showsCancelButton=YES;
            _mySearchBar.delegate=self;
            [_mySearchBar sizeToFit];
            [_mySearchBar setPlaceholder:@"请输入"];
            [_mySearchBar setY:20];
            
            //处理cannel的文字显示
            for (id item in [_mySearchBar subviews]) {
                for(id cc in [item subviews])
                {
                    if ([cc isKindOfClass:[UIButton class]]) {
                        UIButton *btn=(UIButton *)cc;
                        [btn setTitle:@"取消" forState:UIControlStateNormal];
                    }
                }
            }
        }
        return _mySearchBar;
    }
    
    如果是获得瞧点才显示出取消可以在这个委托里面进行设置:
    
    /**
     *  @author wujunyang, 15-06-24 11:06:44
     *
     *  @brief  修改cancel的显示文字 必先把showscancelButton设置为yes
     *  @param searchBar <#searchBar description#>
     */
    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
        searchBar.showsCancelButton=YES;
        for (id item in [searchBar subviews]) {
            for(id cc in [item subviews])
            {
            if ([cc isKindOfClass:[UIButton class]]) {
                UIButton *btn=(UIButton *)cc;
                [btn setTitle:@"取消" forState:UIControlStateNormal];
            }
            }
        }
    }

     还有另外一种方式:

    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
    {
        searchController.searchBar.showsCancelButton = YES;
        UIButton *canceLBtn = [searchController.searchBar valueForKey:@"cancelButton"];
        [canceLBtn setTitle:@"取消" forState:UIControlStateNormal];
        [canceLBtn setTitleColor:[UIColor colorWithRed:14.0/255.0 green:180.0/255.0 blue:0.0/255.0 alpha:1.00] forState:UIControlStateNormal];
        searchBar.showsCancelButton = YES;
        return YES;
    }

    5:关于navigationController中增加控件时push跳转及跳回

    在子页navigationController增加控件,回跳时它是没办法自个销除,所以要手动增加一个销除nav所增加的控件,否则子页的那个控件会被重叠显示在父页的nav上;如下一个实例:
    
    在viewDidLoad里
    //加载控件
        [self.navigationController.view addSubview:self.mySearchBar];
    
    (void)viewWillDisappear:(BOOL)animated {
        //这句也可以写在回跳前
        [self.mySearchBar removeFromSuperview];
        [super viewWillDisappear:animated];
    }

     

    6:整个视图点击都对键盘进行收缩

    - (void)viewDidLoad {
        [super viewDidLoad];
        UITapGestureRecognizer *tapGr=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
        //如果没有这句在view中的Button等可能无法触发ToucheUpInside事件
        tapGr.cancelsTouchesInView=NO;
        [self.view addGestureRecognizer:tapGr];
    }
    - (IBAction)BtnAction:(id)sender {
        NSLog(@"%@",self.myTextField.text);
    }
    -(void)viewTapped:(UITapGestureRecognizer *)tapGr
    {
        [self.myTextField resignFirstResponder];
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    @end

     

    7:针对第三方插件为mrc,而工程为arc的调用

    对第三方插件的.m文件进行设置,工程targets-build phases-compile sources 设置-fno-objc-arc
    
    有些是双星如 PLTexture **previewTextures; 
    在arc下面则要修改成:PLTexture * __unsafe_unretained *previewTextures;

     

    8:通知的方式实现键盘的收缩布局问题

    /**
     *  添加工具条
     */
    - (void)setupToolbar
    {
        // 1.添加工具条
        IWComposeToolbar *toolbar = [[IWComposeToolbar alloc] init];
        toolbar.delegate = self;
        CGFloat toolbarH = 35;
        CGFloat toolbarW = self.view.width;
        CGFloat toolbarY = self.view.height - toolbarH;
        toolbar.frame = CGRectMake(0, toolbarY, toolbarW, toolbarH);
        [self.view addSubview:toolbar];
        self.toolbar = toolbar;
        
        // 2.监听键盘的弹出和隐藏
        // 键盘的frame(位置)即将改变, 就会发出UIKeyboardWillChangeFrameNotification
        // 键盘即将弹出, 就会发出UIKeyboardWillShowNotification
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        // 键盘即将隐藏, 就会发出UIKeyboardWillHideNotification
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    
    
    #pragma mark - 键盘处理
    /**
     *  键盘即将隐藏
     */
    - (void)keyboardWillHide:(NSNotification *)note
    {
        // 1.键盘弹出需要的时间
        CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        
        // 2.动画
        [UIView animateWithDuration:duration animations:^{
            self.toolbar.transform = CGAffineTransformIdentity;
        }];
    }
    
    /**
     *  键盘即将弹出
     */
    - (void)keyboardWillShow:(NSNotification *)note
    {
        // 1.键盘弹出需要的时间
        CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        
        // 2.动画
        [UIView animateWithDuration:duration animations:^{
            // 取出键盘高度
            CGRect keyboardF = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
            CGFloat keyboardH = keyboardF.size.height;
            self.toolbar.transform = CGAffineTransformMakeTranslation(0, - keyboardH);
        }];
    }
    
    //通知要销掉
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    
    注意:[self.textView resignFirstResponder];放弃瞧点
    还有可以监听输入内容的变化:    // 2.监听textView文字的改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:textView];

     

    9:封装一个uivew带有按键工具栏的实例

    .h文件内容:
    
    #import <UIKit/UIKit.h>
    
    @class IWComposeToolbar;
    
    typedef enum {
        IWComposeToolbarButtonTypeCamera,
        IWComposeToolbarButtonTypePicture,
        IWComposeToolbarButtonTypeMention,
        IWComposeToolbarButtonTypeTrend,
        IWComposeToolbarButtonTypeEmotion
    } IWComposeToolbarButtonType;
    
    @protocol IWComposeToolbarDelegate <NSObject>
    @optional
    - (void)composeToolbar:(IWComposeToolbar *)toolbar didClickButton:(IWComposeToolbarButtonType)butonType;
    @end
    
    @interface IWComposeToolbar : UIView
    @property (weak, nonatomic) id<IWComposeToolbarDelegate> delegate;
    @end
    
    
    .m文件内容:
    
    #import "IWComposeToolbar.h"
    
    @implementation IWComposeToolbar
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // 1.设置背景
            self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];
            
            // 2.添加按钮
            [self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:IWComposeToolbarButtonTypeCamera];
            [self addButtonWithIcon:@"compose_toolbar_picture" highIcon:@"compose_toolbar_picture_highlighted" tag:IWComposeToolbarButtonTypePicture];
            [self addButtonWithIcon:@"compose_mentionbutton_background" highIcon:@"compose_mentionbutton_background_highlighted" tag:IWComposeToolbarButtonTypeMention];
            [self addButtonWithIcon:@"compose_trendbutton_background" highIcon:@"compose_trendbutton_background_highlighted" tag:IWComposeToolbarButtonTypeTrend];
            [self addButtonWithIcon:@"compose_emoticonbutton_background" highIcon:@"compose_emoticonbutton_background_highlighted" tag:IWComposeToolbarButtonTypeEmotion];
        }
        return self;
    }
    
    - (void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(IWComposeToolbarButtonType)tag
    {
        UIButton *button = [[UIButton alloc] init];
        button.tag = tag;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
        [button setImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];
        [self addSubview:button];
    }
    
    /**
     *  监听按钮点击
     */
    - (void)buttonClick:(UIButton *)button
    {
        if ([self.delegate respondsToSelector:@selector(composeToolbar:didClickButton:)]) {
            [self.delegate composeToolbar:self didClickButton:button.tag];
        }
    }
    
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        
        int count = self.subviews.count;
        CGFloat buttonW = self.width / count;
        CGFloat buttonH = self.height;
        for (int i = 0; i<count; i++) {
            UIButton *button = self.subviews[i];
            CGFloat buttonX = buttonW * i;
            button.frame = CGRectMake(buttonX, 0, buttonW, buttonH);
        }
    }
    
    @end
  • 相关阅读:
    Ananagrams(反片语)【map的应用】P114
    并查集初步——
    预处理器,编译器,汇编器,连接器
    https://pta.patest.cn/pta/test/15/exam/3/question/724
    stl入门--reverse函数
    无向图的深度遍历
    01背包
    动态规划——数字三角形(递归or递推or记忆化搜索)
    八皇后问题
    幂集的计算
  • 原文地址:https://www.cnblogs.com/wujy/p/4612771.html
Copyright © 2011-2022 走看看