Xcode中的代码片段默认放在下面的目录中:
~/Library/Developer/Xcode/UserData/CodeSnippets
我们可以将目录中的代码片段备份,也可以将其直接拷出来放在不同的电脑上使用
前提:导入Masonry和UI+Addation类库
格式:编号-解释(快捷键)
类库下载地址:https://github.com/mogulanyang/addation
目录:
01-普通View (xView)
01.1 - 带点击事件的View(xViewAction)
02-普通label(xLabel)
02.2-带事件label(xLabelAction)
03-普通textField(xTextField)
04-带blockBtn(xBtn)
05-上图下文字Btn(xBtnFuwenben)
06-初始化tableview(xTableView)
06.1-设置tableView数据源代理(xTableViewData)
07-普通ImageView(xImageView)
07.2-带事件的imageView(xImageViewAction)
07.3-将Image保存到本地相册
08-collectionViewFlowLayout(xFlowLayout)
08.1- collectionView初始化(xCollectionView)
08.2-collectionView的数据源代理方法(xCollectionViewData)
09.2-导航栏全屏pop手势返回(xAllScreenPop)
01-普通View (xView)
({
UIView*view = [[UIViewalloc]init];
[<#code#> addSubview:view];
view.backgroundColor = [UIColor <#code#>];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
view;
});
01.1 - 带点击事件的View(xViewAction)
({
UIView*view = [[UIViewalloc]init];
[<#code#> addSubview:view];
view.backgroundColor = [UIColor <#code#>];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
[view whenTapped:<#^(void)block#>];
view;
});
02-普通label(xLabel)
({
UILabel*label = [[UILabelalloc]init];
label.text = <#code#>;
label.textColor = [UIColor <#code#>];
label.font = [UIFont systemFontOfSize:<#code#>];
[<#code#> addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
label;
});
02.2-带事件label(xLabelAction)
({
UILabel*label = [[UILabelalloc]init];
label.text = <#code#>;
label.textColor = [UIColor <#code#>];
label.font = [UIFont systemFontOfSize:<#code#>];
[<#code#> addSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
[label whenTapped:<#^(void)block#>];
label;
});
03-普通textField(xTextField)
({
UITextField*textField = [[UITextFieldalloc]init];
textField.placeholder=@"<#code#>";
textField.borderStyle=UITextBorderStyleRoundedRect;
textField.font = [UIFont fontWithName:@"Arial"size:<#code#>];//设置属性
//textField.clearButtonMode = UITextFieldViewModeAlways; //带X模式
//textField.keyboardType = UIKeyboardTypeNumberPad; //键盘模式
//textField.secureTextEntry = YES; //密码模式
[<#code#> addSubview:textField];
[textField mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
textField;
});
04-带blockBtn(xBtn)
({
UIButton*btn = [UIButton createBtnFrame:CGRectZero title:<#code#> imageName:<#code#> bgImageName:nilaction:^(UIButton *button) {
<#code#>
}];
[<#code#> addSubview:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
btn;
});
05-上图下文字Btn(xBtnFuwenben)
({
UIButton*button = [UIButtoncreateBtnFrame:CGRectZerotitle:nilimageName:nilbgImageName:nilaction:^(UIButton*button) {
<#code#>
}];
NSAttributedString*str = [NSAttributedString ly_imageTextWithImage:[UIImage imageNamed:@"<#code#>"] imageWH:<#code#> title:@"<#code#>"fontSize:<#code#> titleColor:[UIColor <#code#>] spacing:7];
[buttonsetAttributedTitle:strforState:UIControlStateNormal];
[buttonsizeToFit];
button.titleLabel.numberOfLines=0;
button.titleLabel.textAlignment=NSTextAlignmentCenter;
[<#code#> addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
button;
});
06-初始化tableview(xTableView)
({
//去遵守代理
UITableView*tableView = [[UITableViewalloc]initWithFrame:CGRectZerostyle:UITableViewStylePlain];
[<#code#> addSubview:tableView];
tableView.dataSource=self;
tableView.delegate=self;
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
[tableView registerNib:[UINib nibWithNibName:<#code#> bundle:nil] forCellReuseIdentifier:<#code#>];
[tableView registerClass:[<#code#> class] forCellReuseIdentifier:<#code#>];
tableView;
});
06.1-设置tableView数据源代理(xTableViewData)
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
return1;
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return<#expression#>;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
<#classCell#> * cell = [tableView dequeueReusableCellWithIdentifier:<#(nonnull NSString *)#>];
returncell;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
}
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
return<#expression#>;
}
07-普通ImageView(xImageView)
({
UIImageView*imageView = [[UIImageViewalloc]init];
imageView.image = [UIImage imageNamed:<#code#>];
imageView.contentMode=UIViewContentModeScaleAspectFill;
[<#code#> addSubview:imageView];
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
imageView;
});
07.1-带点击事件的ImageView(xImageViewAction)
({
UIImageView*imageView = [[UIImageViewalloc]init];
imageView.image = [UIImage imageNamed:<#code#>];
imageView.contentMode=UIViewContentModeScaleAspectFill;
[<#code#> addSubview:imageView];
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
[imageView whenTapped:<#^(void)block#>];
imageView;
});
07.3-将Image保存到本地相册
#pragma mark -保存相册:image调用此方法即可调用
- (void)saveImageToPhotos:(UIImage*)savedImage
{
UIImageWriteToSavedPhotosAlbum(savedImage,self,@selector(image:didFinishSavingWithError:contextInfo:),NULL);
}
//图片保存成功
- (void)image: (UIImage*) image didFinishSavingWithError: (NSError*) error contextInfo: (void*) contextInfo
{
NSString*msg =nil;
if(error !=NULL){
msg =@"保存图片失败";
}else{
msg =@"保存图片成功";
}
UILabel*saveHud =({
UILabel*label = [[UILabelalloc]init];
label.text=@"图片已经保存成功";
label.textAlignment=NSTextAlignmentCenter;
label.textColor= [UIColorwhiteColor];
label.font= [UIFontsystemFontOfSize:12];
label.backgroundColor= [UIColorcolorWithWhite:0.6alpha:0.7];
[self.viewaddSubview:label];
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.top.equalTo(self.view).offset(20);
}];
label;
});
[UIViewanimateWithDuration:1.5animations:^{
saveHud.alpha=0;
}completion:^(BOOLfinished) {
[saveHudremoveFromSuperview];
}];
}
08-collectionViewFlowLayout(xFlowLayout)
- (void)prepareLayout
{
[superprepareLayout];
self.scrollDirection = UICollectionViewScrollDirection<#code#>;
self.minimumLineSpacing =0;
self.minimumInteritemSpacing =0;
self.itemSize = CGSizeMake(self.collectionView.bounds.size.width / <#code#>,self.collectionView.bounds.size.height / <#code#>);
self.collectionView.showsVerticalScrollIndicator =NO;
self.collectionView.showsHorizontalScrollIndicator =NO;
self.collectionView.backgroundColor = [UIColor <#code#>];
self.sectionInset = UIEdgeInsetsMake(0,0,0,0);
self.collectionView.pagingEnabled = <#code#>;
}
08.1- collectionView初始化(xCollectionView)
({
<#code#> *flowLayout = [[<#code#> alloc] init];
UICollectionView*collectionView = [[UICollectionViewalloc]initWithFrame:CGRectZerocollectionViewLayout:flowLayout];
[<#code#> addSubview:collectionView];
collectionView.dataSource=self;
collectionView.delegate=self;
collectionView.backgroundColor = [UIColor <#code#>];
[collectionView registerClass:[<#code#> class] forCellWithReuseIdentifier:<#code#>];
[collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
<#code#>
}];
collectionView;
});
08.2-collectionView的数据源代理方法(xCollectionViewData)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView
{
return1;
}
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section
{
return<#expression#>;
}
- (__kindofUICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
<#expression#> *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:@"<#code#>"forIndexPath:indexPath];
returncell;
}
- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
<#code#>;
}
- (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath
{
return<#expression#>;
}
09.2-导航栏全屏pop手势返回(xAllScreenPop)
#pragma mark -导航栏全屏返回
#warning记得遵守UIGestureRecognizerDelegate方法
- (void)allScreenPop
{
UIGestureRecognizer*gesture =self.interactivePopGestureRecognizer;
//把这个手势禁掉严禁使用
NSLog(@"%@",gesture);
gesture.enabled=NO;
//获取手势对应的view
UIView*gestureView = gesture.view;
//创建一个拖拽的手势
UIPanGestureRecognizer*popRecognizer = [[UIPanGestureRecognizeralloc]init];
popRecognizer.delegate=self;
//添加在被拖拽的view上面即:上面的view
[gestureViewaddGestureRecognizer:popRecognizer];
/**
*获取系统手势的target数组
*/
NSMutableArray*_targets = [gesturevalueForKey:@"_targets"];
/**
*获取它的唯一对象,我们知道它是一个叫UIGestureRecognizerTarget的私有类,它有一个属性叫_target
*/
idgestureRecognizerTarget = [_targetsfirstObject];
/**
*获取_target:_UINavigationInteractiveTransition,它有一个方法叫handleNavigationTransition:
*/
idnavigationInteractiveTransition = [gestureRecognizerTargetvalueForKey:@"_target"];
/**
*通过前面的打印,我们从控制台获取出来它的方法签名。
*/
SELhandleTransition =NSSelectorFromString(@"handleNavigationTransition:");
/**
*创建一个与系统一模一样的手势,我们只把它的类改为UIPanGestureRecognizer
*/
[popRecognizeraddTarget:navigationInteractiveTransitionaction:handleTransition];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer*)gestureRecognizer {
/**
*这里有两个条件不允许手势执行,1、当前控制器为根控制器;2、如果这个push、pop动画正在执行(私有属性)
*/
returnself.viewControllers.count!=1&& ![[selfvalueForKey:@"_isTransitioning"]boolValue];
})
#pragma mark -保证跳转以后Tabbar不在
- (void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated
{
if(self.viewControllers.count>0) {
viewController.hidesBottomBarWhenPushed=YES;
}
[superpushViewController:viewControlleranimated:YES];
}