比例适配,简单实现等比例适配

作者:计算机知识
前言

不问可知,masonry是基于autolayout的适配,使用起来依然绝对来讲比较容易的。可是出于自家更趋势于比例适配(因为中间学过一段时间Android,对Android中的比例适配成对比赏识卡塔尔所以长期以来都用比例适配,近日几天朋友给本人了三个要好写的demo,适逢其时借着今日刚上学的UIResponder给我们讲授一下以此demo,希望对我们有帮衬。在那也对本人这一个朋友,表示名贵的珍贵。

正文转自二个有情侣 Peak_One自感觉写的真不错,决断转了!!!

亚洲必赢登陆网址,0.在appdelegate中扬言三个变量

@property (nonatomic , assign)float autoSizeScaleX;
@property (nonatomic , assign)float autoSizeScaleY;

在UITextView(scrollview的子类卡塔尔内部加一个label,必得那样适配,

iOS中的CGRectmake方法讲授

比例适配,简单实现等比例适配。率先大家得以依靠CGRectmake方法跳转至CG吉优metry.h能够见见大家日常所用的一些常用方法,举个例子:

CGPoint CGPointMake(CGFloat x, CGFloat y);CGSize CGSizeMake(CGFloat width, CGFloat height);CGRect CGRectMake(CGFloat x, CGFloat y, CGFloat width,CGFloat height);

对此那些措施,不做太多解释,相信假设爬一下文书档案,就能够轻易的运用。这里吧,借图轻松的求证一下。

亚洲必赢登陆网址 1CGGeometry.png

原稿链接:

1.在appdelegate.m中初阶化那多少个变量

/// 获取到delegate
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
/// 这里是基于苹果6sp的尺寸为标准,其他屏幕按比例缩放
    if (kScreenHeight > 苹果6sp){
        delegate.autoSizeScaleX = 1;
        delegate.autoSizeScaleY = 1;
    } else {
        delegate.autoSizeScaleX = kScreenWidth / 375;
        delegate.autoSizeScaleY = kScreenHeight / 667;
    }
[label makeConstraints:^(MASConstraintMaker *make) {    
  make.top.equalTo(textView.mas_top).offset(123);
  make.height.equalTo(12); 
  make.bottom.equalTo(textView).offset(-10); 

  make.left.equalTo(textView).offset(kSCREEN_WIDTH-30-50);
  make.width.equalTo(40);
  make.right.equalTo(textView).offset(-10);
}];
// 当textView的输入字数过多时,即contentsize变了,label的位置也会变。
// 遇到这种情况,在textView的内部加一个containerView,containerView与textView适配,label在加在containerView中。
// 此方法也适合复杂的scrollView,更为便利。
// 或者吧textView放进view中,再把label加在View中,这样textView和label成为兄弟视图。
思考

那么看看这里,如何写多个适配文件,供全局调用呢?其实写一个category去扩充那一个类,然后去增多三个pch文件,在pch文件中#import就足以兑现调用。其实那标准的考虑就相比局限了,大家能够试想,日常去设置坐标的绝对是叁个UIView,那么是否一旦去扩充UIView的父类,就足以让具备的UIView去遵照category中的方法进行结构?答案是一定的。所早前几日介绍的UIResponder便派上了用途。大家能够去扩充UIResponder去完毕结构。废话十分少说,上代码:UIResponder Adapt.h

/** 需要适配的地方 替换 CGRectMake => CGRectMakeAdapt  e.g. button.frame = CGRectMake(100, 100, 100, 100); => button.frame = CGRectMakeAdapt(100, 100, 100, 100) */#import <UIKit/UIKit.h>/** 适配 与设计图物理宽度比较 */@interface UIResponder #pragma mark - 默认模式为iPhone5的设计图/*! @brief 水平比例适配 @param level 原值 @return 适配后的值 */CGFloat Adapt_scaleL(CGFloat level);/*! @brief 竖直比例适配 取值为水平比例适配 @param level 原值 @return 适配后的值 */CGFloat Adapt_scaleV(CGFloat vertical);/** 适配CGpoint */CGPoint CGPointMakeAdapt(CGFloat x, CGFloat y);/** 适配CGSize */CGSize CGSizeMakeAdapt(CGFloat width, CGFloat height);/** 适配CGRect */CGRect CGRectMakeAdapt(CGFloat x, CGFloat y, CGFloat width, CGFloat height);/** 适配UIEdgeInsets */UIEdgeInsets UIEdgeInsetsMakeAdapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);#pragma mark - 选择适配方式#pragma mark 设计图为IPHONE4CGFloat Adapt_IPHONE4_scaleL(CGFloat level);CGFloat Adapt_IPHONE4_scaleV(CGFloat vertical);CGPoint CGPointMakeIPHONE4Adapt(CGFloat x, CGFloat y);CGSize CGSizeMakeIPHONE4Adapt(CGFloat width, CGFloat height);CGRect CGRectMakeIPHONE4Adapt(CGFloat x, CGFloat y, CGFloat width, CGFloat height);UIEdgeInsets UIEdgeInsetsMakeIPHONE4Adapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);#pragma mark 设计图为IPHONE5/5sCGFloat Adapt_IPHONE5_scaleL(CGFloat level);CGFloat Adapt_IPHONE5_scaleV(CGFloat vertical);CGPoint CGPointMakeIPHONE5Adapt(CGFloat x, CGFloat y);CGSize CGSizeMakeIPHONE5Adapt(CGFloat width, CGFloat height);CGRect CGRectMakeIPHONE5Adapt(CGFloat x, CGFloat y, CGFloat width, CGFloat height);UIEdgeInsets UIEdgeInsetsMakeIPHONE5Adapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);#pragma mark 设计图为IPHONE6CGFloat Adapt_IPHONE6_scaleL(CGFloat level);CGFloat Adapt_IPHONE6_scaleV(CGFloat vertical);CGPoint CGPointMakeIPHONE6Adapt(CGFloat x, CGFloat y);CGSize CGSizeMakeIPHONE6Adapt(CGFloat width, CGFloat height);CGRect CGRectMakeIPHONE6Adapt(CGFloat x, CGFloat y,CGFloat width, CGFloat height);UIEdgeInsets UIEdgeInsetsMakeIPHONE6Adapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);#pragma mark 设计图为IPHONE6PCGFloat Adapt_IPHONE6P_scaleL(CGFloat level);CGFloat Adapt_IPHONE6P_scaleV(CGFloat vertical);CGPoint CGPointMakeIPHONE6PAdapt(CGFloat x, CGFloat y);CGSize CGSizeMakeIPHONE6PAdapt(CGFloat width, CGFloat height);CGRect CGRectMakeIPHONE6PAdapt(CGFloat x, CGFloat y, CGFloat width, CGFloat height);UIEdgeInsets UIEdgeInsetsMakeIPHONE6PAdapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);@end

UIResponder Adapt.m#import <Foundation/Foundation.h>

#pragma mark - 枚举 设计图类型/** 适配类型 设计图类型 */typedef NS_ENUM(NSInteger, kAdaptModelType) { /** 5/5s设计图适配 */kAdaptTypeIPHONE5 = 0 ,/** 6设计图适配 */kAdaptTypeIPHONE6 = 1,/** 6 设计图适配 */kAdaptTypeIPHONE6P = 2,/** 4设计图适配 */kAdaptTypeIPHONE4 = 3};#define IPHONE6_P_Height 736#define IPHONE6_P_Wideth 414#define IPHONE6_Height 667#define IPHONE6_Wideth 375#define IPHONE5_Height 568#define IPHONE5_Wideth 320#define IPHONE4_Height 480#define IPHONE4_Wideth 320/** 比例数据 供参考 6p/6 width: 736/667 = 1.103448 height:414/375 = 1.104 6/5 width: 667/568 = 1.174296 height:375/320 = 1.171875 6p/5 width: 736/568 = 1.295775 height:414/320 = 1.29 5/4 width: 568/480 = 1.183333 height:320/320 = 1.0 // 选择适配方法为 : 比例尺 = 设备屏幕物理尺寸宽/设计图物理尺寸宽  */#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)/** 获取分辨率尺寸 */#define SCREEN_SCALE_WIDTH [[UIScreen mainScreen] currentMode].size.width;#define SCREEN_SCALE_HEIGHT [[UIScreen mainScreen] currentMode].size.height;/** 获取物理屏尺寸 */#define ZM_SCREENWIDTH [[UIScreen mainScreen]bounds].size.width#define ZM_SCREENHEIGHT [[UIScreen mainScreen]bounds].size.height// 比例#define Adapt_PROPORTION_IPHONE_4_WIDTH ZM_SCREENWIDTH / IPHONE4_Wideth#define Adapt_PROPORTION_IPHONE_4_HEIGHT ZM_SCREENHEIGHT / IPHONE4_Height#define Adapt_PROPORTION_IPHONE_5_WIDTH ZM_SCREENWIDTH /IPHONE5_Wideth#define Adapt_PROPORTION_IPHONE_5_HEIGHT ZM_SCREENHEIGHT /IPHONE5_Height#define Adapt_PROPORTION_IPHONE_6_WIDTH ZM_SCREENWIDTH / IPHONE6_Wideth#define Adapt_PROPORTION_IPHONE_6_HEIGHT ZM_SCREENHEIGHT / IPHONE6_Height#define Adapt_PROPORTION_IPHONE_6_PLUS_WIDTH ZM_SCREENWIDTH / IPHONE6_P_Wideth#define Adapt_PROPORTION_IPHONE_6_PLUS_HEIGHT ZM_SCREENHEIGHT / IPHONE6_P_Height#import "UIResponder Adapt.h"@implementation UIResponder // 比例尺kAdaptModelType kAdaptType = kAdaptTypeIPHONE5;#pragma mark - 默认选择iPhone5设计图模板CGPoint CGPointMakeAdapt(CGFloat x, CGFloat y){ kAdaptType = kAdaptTypeIPHONE5; return AdaptCGPointMake;}CGSize CGSizeMakeAdapt(CGFloat width, CGFloat height){ kAdaptType = kAdaptTypeIPHONE5; return AdaptCGSizeMake( width , height );}CGRect CGRectMakeAdapt(CGFloat x, CGFloat y, CGFloat width, CGFloat height){ kAdaptType = kAdaptTypeIPHONE5; return AdaptCGRectMake(x, y, width, height);}UIEdgeInsets UIEdgeInsetsMakeAdapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right){ kAdaptType = kAdaptTypeIPHONE5; return AdaptUIEdgeInsetsMake( top, left, bottom, right);}#pragma mark - 选择适配方式#pragma mark 设计图为IPHONE4CGFloat Adapt_IPHONE4_scaleL(CGFloat level){ kAdaptType = kAdaptTypeIPHONE4; return Adapt_scaleL;}CGFloat Adapt_IPHONE4_scaleV(CGFloat vertical){ kAdaptType = kAdaptTypeIPHONE4; return Adapt_scaleV;}CGPoint CGPointMakeIPHONE4Adapt(CGFloat x, CGFloat y){ kAdaptType = kAdaptTypeIPHONE4; return AdaptCGPointMake;}CGSize CGSizeMakeIPHONE4Adapt(CGFloat width, CGFloat height){ kAdaptType = kAdaptTypeIPHONE4; return AdaptCGSizeMake( width , height );}CGRect CGRectMakeIPHONE4Adapt(CGFloat x, CGFloat y, CGFloat width, CGFloat height){ kAdaptType = kAdaptTypeIPHONE4; return AdaptCGRectMake(x, y, width, height);}UIEdgeInsets UIEdgeInsetsMakeIPHONE4Adapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right){ kAdaptType = kAdaptTypeIPHONE4; return AdaptUIEdgeInsetsMake( top, left, bottom, right);}#pragma mark 设计图为IPHONE5/5sCGFloat Adapt_IPHONE5_scaleL(CGFloat level){ kAdaptType = kAdaptTypeIPHONE5; return Adapt_scaleL;}CGFloat Adapt_IPHONE5_scaleV(CGFloat vertical){ kAdaptType = kAdaptTypeIPHONE5; return Adapt_scaleV;}CGPoint CGPointMakeIPHONE5Adapt(CGFloat x, CGFloat y){ kAdaptType = kAdaptTypeIPHONE5; return AdaptCGPointMake; }CGSize CGSizeMakeIPHONE5Adapt(CGFloat width, CGFloat height){ kAdaptType = kAdaptTypeIPHONE5; return AdaptCGSizeMake( width , height );}CGRect CGRectMakeIPHONE5Adapt(CGFloat x, CGFloat y, CGFloat width, CGFloat height){ kAdaptType = kAdaptTypeIPHONE5; return AdaptCGRectMake(x, y, width, height);}UIEdgeInsets UIEdgeInsetsMakeIPHONE5Adapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right){ kAdaptType = kAdaptTypeIPHONE5; return AdaptUIEdgeInsetsMake( top, left, bottom, right);}#pragma mark 设计图为IPHONE6CGFloat Adapt_IIPHONE6_scaleL(CGFloat level){ kAdaptType = kAdaptTypeIPHONE6; return Adapt_scaleL;}CGFloat Adapt_IPHONE6_scaleV(CGFloat vertical){ kAdaptType = kAdaptTypeIPHONE6; return Adapt_scaleV;}CGPoint CGPointMakeIPHONE6Adapt(CGFloat x, CGFloat y){ kAdaptType = kAdaptTypeIPHONE6; return AdaptCGPointMake;}CGSize CGSizeMakeIPHONE6Adapt(CGFloat width, CGFloat height){ kAdaptType = kAdaptTypeIPHONE6; return AdaptCGSizeMake( width , height );}CGRect CGRectMakeIPHONE6Adapt(CGFloat x, CGFloat y, CGFloat width, CGFloat height){ kAdaptType = kAdaptTypeIPHONE6; return AdaptCGRectMake(x, y, width, height);}UIEdgeInsets UIEdgeInsetsMakeIPHONE6Adapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right){ kAdaptType = kAdaptTypeIPHONE6; return AdaptUIEdgeInsetsMake( top, left, bottom, right);}#pragma mark 设计图为IPHONE6PCGFloat Adapt_IPHONE6P_scaleL(CGFloat level){ kAdaptType = kAdaptTypeIPHONE6P; return Adapt_scaleL;}CGFloat Adapt_IPHONE6P_scaleV(CGFloat vertical){ kAdaptType = kAdaptTypeIPHONE6P; return Adapt_scaleV;}CGPoint CGPointMakeIPHONE6PAdapt(CGFloat x, CGFloat y){ kAdaptType = kAdaptTypeIPHONE6P; return AdaptCGPointMake;}CGSize CGSizeMakeIPHONE6PAdapt(CGFloat width, CGFloat height){ kAdaptType = kAdaptTypeIPHONE6P; return AdaptCGSizeMake( width , height );}CGRect CGRectMakeIPHONE6PAdapt(CGFloat x, CGFloat y, CGFloat width, CGFloat height){ kAdaptType = kAdaptTypeIPHONE6P; return AdaptCGRectMake(x, y, width, height);}UIEdgeInsets UIEdgeInsetsMakeIPHONE6PAdapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right){ kAdaptType = kAdaptTypeIPHONE6P; return AdaptUIEdgeInsetsMake( top, left, bottom, right);}#pragma mark - 适配#pragma mark scale//CGFloat scale_L(CGFloat level) {return Adapt_scaleL;}//CGFloat scale_V(CGFloat vertical) {return Adapt_scaleV;}CGFloat Adapt_scaleL(CGFloat level){CGFloat scale_L = Adapt_PROPORTION_IPHONE_5_WIDTH;switch (kAdaptType) { case kAdaptTypeIPHONE4: scale_L = Adapt_PROPORTION_IPHONE_4_WIDTH; break; case kAdaptTypeIPHONE6: scale_L = Adapt_PROPORTION_IPHONE_6_WIDTH; break; case kAdaptTypeIPHONE6P: scale_L = Adapt_PROPORTION_IPHONE_6_PLUS_WIDTH; break; default: break;} return level*scale_L;}CGFloat Adapt_scaleV(CGFloat vertical){ return Adapt_scaleL;}#pragma mark adaptCGPoint AdaptCGPointMake(CGFloat x, CGFloat y){ return CGPointMake(Adapt_scaleL, Adapt_scaleV;}CGSize AdaptCGSizeMake(CGFloat width, CGFloat height){ return CGSizeMake( Adapt_scaleL , Adapt_scaleV;}CGRect AdaptCGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height){ return CGRectMake(Adapt_scaleL, Adapt_scaleV, Adapt_scaleL, Adapt_scaleV;}UIEdgeInsets AdaptUIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right){ return UIEdgeInsetsMake(Adapt_scaleV, Adapt_scaleL, Adapt_scaleV, Adapt_scaleL;}@end

这几个代码其实简单,不过收拾出来照旧需求一番素养的,所以理应向本人的那位朋友致敬:曾墨引用超轻便,首先得导入头文件,在那处呢,建议创制pch文件二遍性导入,然后呢,你会发觉,你能够透过相对结构,完毕全体显示器的适配,最重视的是:方法还跟从前一流相似:首要调用那多少个措施:CGPoint CGPointMakeAdapt(CGFloat x, CGFloat y卡塔尔(قطر‎;CGSize CGSizeMakeAdapt(CGFloat width, CGFloat height卡塔尔;CGRect CGRectMake艾达pt(CGFloat x, CGFloat y, CGFloat width, CGFloat height);UIEdgeInsets UIEdgeInsetsMakeAdapt(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right卡塔尔;

款待关怀自小编的村办Wechat公众号,免费送计算机各个新型录制能源!你想像不到的上佳!

亚洲必赢登陆网址 20.jpg

小说权归小编全部!!

2.在pch中 获取到appdelegate

#define ZCAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

前言

3.做成你自个儿的宏,调用的时候一贯adaptX(num卡塔尔(قطر‎或然adaptY(num卡塔尔

#define adaptX(num) num *ZCAppDelegate.autoSizeScaleX
#define adaptY(num) num *ZCAppDelegate.autoSizeScaleY

不言而谕,masonry是基于autolayout的适配,使用起来还是相对来讲比较容易的。可是出于自个儿更赞成于比例适配(因为中间学过一段时间Android,对Android中的比例适配相比较赏识卡塔尔国所以直接以来都用比例适配,近来几天朋友给自己了二个协调写的demo,刚巧借着今日刚上学的UIResponder给大家批注一下以此demo,希望对大家有协理。在那也对自己那些心上人,表示尊贵的敬慕。

4.谈到底别忘了把appdelegate满含在pch文件里

iOS中的CGRectmake方法讲解

先是大家得以依照CGRectmake方法跳转至CGGeometry.h可以看出大家一直所用的一部分常用方法,举例:

CGPointCGPointMake(CGFloatx,CGFloaty);CGSizeCGSizeMake(CGFloatwidth,CGFloatheight);CGRectCGRectMake(CGFloatx,CGFloaty,CGFloatwidth,CGFloatheight);

对此这几个方法,不做太多解释,相信借使爬一下文书档案,就足以简简单单的选取。这里吧,借图简来讲之惠氏下。

亚洲必赢登陆网址 3

本文由bwin必赢发布,转载请注明来源

关键词: iOS 简单 比例 iOS 测试 && 性能