首页 文章 精选 留言 我的

精选列表

搜索[ios],共10000篇文章
优秀的个人博客,低调大师

(转载)iOS - layoutSubviews、drawRect、awakeFromNib和 loadNibNamed解释

原文地址:http://blog.csdn.net/xiaohe901216/article/details/50380625 awakeFromNib 当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的 awakeFromNib函数来响应这个消息,执行一些必要的操作。也就是说通过nib文件创建view对象是执行awakeFromNib 。 viewDidLoad 当view对象被加载到内存是就会执行viewDidLoad,所以不管通过nib文件还是代码的方式创建对象都会执行viewDidLoad。 layoutSubviews layoutSubviews方便数据计算,drawRect方便视图重绘。 layoutSubviews在以下情况下会被调用: 1、init初始化不会触发layoutSubviews。 2、addSubview会触发layoutSubviews。 3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化。 4、滚动一个UIScrollView会触发layoutSubviews。 5、旋转Screen会触发父UIView上的layoutSubviews事件。 6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件。 7、直接调用setLayoutSubviews。 drawRect drawRect在以下情况下会被调用: 1、如果在UIView初始化时没有设置rect大小,将直接导致drawRect不被自动调用。drawRect 掉用是在Controller->loadView,?Controller->viewDidLoad?两方法之后掉用的.所以不用担心在 控制器中,这些View的drawRect就开始画了.这样可以在控制器中设置一些值给View(如果这些View?draw的时候需要用到某些变量 值). 2、该方法在调用sizeToFit后被调用,所以可以先调用sizeToFit计算出size。然后系统自动调用drawRect:方法。 3、通过设置contentMode属性值为UIViewContentModeRedraw。那么将在每次设置或更改frame的时候自动调用drawRect:。 4、直接调用setNeedsDisplay,或者setNeedsDisplayInRect:触发drawRect:,但是有个前提条件是rect不能为0。 以上1,2推荐;而3,4不提倡 ? drawRect方法使用注意点: 1、 若使用UIView绘图,只能在drawRect:方法中获取相应的contextRef并绘图。如果在其他方法中获取将获取到一个invalidate 的ref并且不能用于画图。drawRect:方法不能手动显示调用,必须通过调用setNeedsDisplay?或 者?setNeedsDisplayInRect,让系统自动调该方法。 2、若使用calayer绘图,只能在drawInContext:?中(类似于drawRect)绘制,或者在delegate中的相应方法绘制。同样也是调用setNeedDisplay等间接调用以上方法 3、若要实时画图,不能使用gestureRecognizer,只能使用touchbegan等方法来调用setNeedsDisplay实时刷新屏幕 initWithNibName initWithNibName这个方法是在controller的类在IB中创建,但是通过Xcode实例化controller的时候用的. initWithCoder initWithCoder 是一个类在IB中创建但在xocde中被实例化时被调用的.比如,通过IB创建一个controller的nib文件,然后在xcode中通过 initWithNibName来实例化这个controller,那么这个controller的initWithCoder会被调用.或者是一个view的nib文件,类似方法创建时调用initWithCoder 关于 initWithNibName 和 loadNibNamed 的区别和联系 之所以要把这两者来一起讲,我觉的我也有点困惑,到底用那种?其实真正搞清楚了他们之间的差别,就不会这么迷惘了。因为这两个方法,根本就不是一路货色。 既然,是要说明这2个方法,那就着重将区别吧。 但是第一步,还是要罗嗦一下,他们的联系:可以使用此方法加载用户界面(xib文件)到我们的代码中,这样,可以通过操作这个加载进来的(xib)对象,来操作xib文件内容。 下面进入主题,谈区别: ShowViewController的initWithNibName方法 ShowViewController * showMessage = [[ShowViewController alloc] initWithNibName:@"ShowViewController" bundle:nil]; self.showViewController = showMessage; 2.VideoCellController的loadNibNamed方法 NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"SaveViewController" owner:self options:nil] ; self.showViewController = [nib lastObject]; [nib objectAtIndex:0]; 总结: 只看他们初始化,那可能感觉是一样的。但是如果,打开分别看xib的关系的时候,才恍然大悟,原来他们的集成类都不一样。 initWithNibName要加载的xib的类为我们定义的视图控制器类 2.加载方式不同 initWithNibName方法:是延迟加载,这个View上的控件是 nil 的,只有到 需要显示时,才会不是 nil loadNibNamed方法:即时加载,用该方法加载的xib对象中的各个元素都已经存在。 (认真理解这句帮规:when using loadNibNamed:owner:options:, the File's Owner should be NSObject, the main view should be your class type, and all outlets should be hooked up to the view, not the File's Owner.) initWithCoder和initWithFrame的区别 initWithoder 是当从nib文件中加载对象的时候会调用,比如你的view来自nib那么就会调用这个view的这个函数。(由框架调用) initWithFrame (是由用户调用,来初始化对象的)

优秀的个人博客,低调大师

iOS - Core Animation 核心动画的使用

1、简单使用示例 1.1 时钟 QClockView.h @interface QClockView : UIView /// 创建时钟界面 + (instancetype)q_clockViewWithFrame:(CGRect)frame; @end QClockView.m #define CLOCK_WIDTH self.bounds.size.width @interface QClockView () // 表盘 @property (nonatomic, strong) UIImageView *clockView; // 指针 @property (nonatomic, strong) CALayer *secondLayer; @property (nonatomic, strong) CALayer *minuteLayer; @property (nonatomic, strong) CALayer *hourLayer; @end @implementation QClockView /// 创建时钟界面 + (instancetype)q_clockViewWithFrame:(CGRect)frame { QClockView *clockView = [[self alloc] initWithFrame:frame]; return clockView; } /// 初始化 - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 添加表盘 self.clockView = [[UIImageView alloc] init]; self.clockView.image = [UIImage imageNamed:@"clock"]; [self addSubview:self.clockView]; // 添加定时器 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES]; [self timeChange]; } return self; } /// 布局子控件 - (void)layoutSubviews { [super layoutSubviews]; self.clockView.frame = self.bounds; } // 定时器响应事件处理 - (void)timeChange { // 获取当前的系统的时间 // 获取当前日历对象 NSCalendar *calendar = [NSCalendar currentCalendar]; // 获取日期的组件:年月日小时分秒 NSDateComponents *cmp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]]; // 获取秒 NSInteger second = cmp.second + 1; // 获取分 NSInteger minute = cmp.minute; // 获取小时 NSInteger hour = cmp.hour; // 计算秒针转多少度,一秒钟秒针转 6° CGFloat secondA = second * 6; // 计算分针转多少度,一分钟分针转 6° CGFloat minuteA = minute * 6; // 计算时针转多少度,一小时时针转 30°,每分钟时针转 0.5° CGFloat hourA = hour * 30 + minute * 0.5; // 旋转秒针,使用 layer 的隐式动画属性产生动画效果 self.secondLayer.transform = CATransform3DMakeRotation(secondA / 180 * M_PI, 0, 0, 1); // 旋转分针 self.minuteLayer.transform = CATransform3DMakeRotation(minuteA / 180 * M_PI, 0, 0, 1); // 旋转小时 self.hourLayer.transform = CATransform3DMakeRotation(hourA / 180 * M_PI, 0, 0, 1); } // 添加秒针 - (CALayer *)secondLayer { if (_secondLayer == nil) { _secondLayer = [CALayer layer]; _secondLayer.backgroundColor = [UIColor redColor].CGColor; _secondLayer.anchorPoint = CGPointMake(0.5, 1); _secondLayer.position = CGPointMake(CLOCK_WIDTH * 0.5, CLOCK_WIDTH * 0.5); _secondLayer.bounds = CGRectMake(0, 0, 1, CLOCK_WIDTH * 0.5 - 20); [self.clockView.layer addSublayer:_secondLayer]; } return _secondLayer; } // 添加分针 - (CALayer *)minuteLayer { if (_minuteLayer == nil) { _minuteLayer = [CALayer layer]; _minuteLayer.backgroundColor = [UIColor blackColor].CGColor; _minuteLayer.anchorPoint = CGPointMake(0.5, 1); _minuteLayer.position = CGPointMake(CLOCK_WIDTH * 0.5, CLOCK_WIDTH * 0.5); _minuteLayer.bounds = CGRectMake(0, 0, 4, CLOCK_WIDTH * 0.5 - 20); _minuteLayer.cornerRadius = 2; [self.clockView.layer addSublayer:_minuteLayer]; } return _minuteLayer; } // 添加时针 - (CALayer *)hourLayer { if (_hourLayer == nil) { _hourLayer = [CALayer layer]; _hourLayer.backgroundColor = [UIColor blackColor].CGColor; _hourLayer.anchorPoint = CGPointMake(0.5, 1); _hourLayer.position = CGPointMake(CLOCK_WIDTH * 0.5, CLOCK_WIDTH * 0.5); _hourLayer.bounds = CGRectMake(0, 0, 4, CLOCK_WIDTH * 0.5 - 40); _hourLayer.cornerRadius = 2; [self.clockView.layer addSublayer:_hourLayer]; } return _hourLayer; } @end ViewController.m // 创建时钟界面 QClockView *clockView = [QClockView q_clockViewWithFrame:CGRectMake(100, 100, 200, 200)]; [self.view addSubview:clockView]; 效果 1.2 转盘 QWheelButton.h @interface QWheelButton : UIButton @end QWheelButton.m @implementation QWheelButton /// 设置按钮中 imageView 的尺寸 - (CGRect)imageRectForContentRect:(CGRect)contentRect { // 计算 imageView 控件尺寸,contentRect 为按钮的尺寸 CGFloat W = 40; CGFloat H = 46; CGFloat X = (contentRect.size.width - W) * 0.5; CGFloat Y = 20; return CGRectMake(X, Y, W, H); } /// 设置按钮接收点击事件的区域 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { CGFloat btnW = self.bounds.size.width; CGFloat btnH = self.bounds.size.height; // 计算不接收点击事件的区域 CGFloat X = 0; CGFloat Y = btnH / 2; CGFloat W = btnW; CGFloat H = Y; CGRect rect = CGRectMake(X, Y, W, H); if (CGRectContainsPoint(rect, point)) { return nil; } else { return [super hitTest:point withEvent:event]; } } @end QWheelView.h @interface QWheelView : UIView /// 创建转盘界面 + (instancetype)q_wheelViewWithFrame:(CGRect)frame; /// 开始转动 - (void)q_startAnimating; /// 停止转动 - (void)q_stopAnimating; @end QWheelView.m #import "QWheelButton.h" #define WHEEL_WIDTH self.bounds.size.width @interface QWheelView () <CAAnimationDelegate> /// 转盘控件 @property (nonatomic, strong) UIImageView *backImageView; @property (nonatomic, strong) UIImageView *centerImagerView; @property (nonatomic, strong) UIButton *startButton; /// 当前选中的按钮 @property (nonatomic, weak) UIButton *selectedButton; /// 显示定时器 @property (nonatomic, strong) CADisplayLink *link; @end @implementation QWheelView /// 创建转盘界面 + (instancetype)q_wheelViewWithFrame:(CGRect)frame { QWheelView *wheelView = [[self alloc] initWithFrame:frame]; return wheelView; } /// 开始转动 - (void)q_startAnimating { self.link.paused = NO; } /// 停止转动 - (void)q_stopAnimating { self.link.paused = YES; } /// 初始化转盘控件 - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 添加转盘界面 self.backImageView = [[UIImageView alloc] init]; self.backImageView.image = [UIImage imageNamed:@"LuckyBaseBackground"]; [self addSubview:self.backImageView]; self.centerImagerView = [[UIImageView alloc] init]; self.centerImagerView.image = [UIImage imageNamed:@"LuckyRotateWheel"]; self.centerImagerView.userInteractionEnabled = YES; [self addSubview:self.centerImagerView]; // 添加开始选号按钮 self.startButton = [[UIButton alloc] init]; [self.startButton setBackgroundImage:[UIImage imageNamed:@"LuckyCenterButton"] forState:UIControlStateNormal]; [self.startButton setBackgroundImage:[UIImage imageNamed:@"LuckyCenterButtonPressed"] forState:UIControlStateHighlighted]; [self.startButton addTarget:self action:@selector(startPicker:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.startButton]; // 添加号码按钮 // 加载大图片 UIImage *bigImage = [UIImage imageNamed:@"LuckyAstrology"]; UIImage *selBigImage = [UIImage imageNamed:@"LuckyAstrologyPressed"]; // 获取当前使用的图片像素和点的比例 CGFloat scale = [UIScreen mainScreen].scale; CGFloat imageW = bigImage.size.width / 12 * scale; CGFloat imageH = bigImage.size.height * scale; for (int i = 0; i < 12; i++) { QWheelButton *button = [QWheelButton buttonWithType:UIButtonTypeCustom]; [self.centerImagerView addSubview:button]; // 设置按钮图片 CGRect clipR = CGRectMake(i * imageW, 0, imageW, imageH); [button setImage:[self getImageWithClipRect:clipR fromImage:bigImage] forState:UIControlStateNormal]; [button setImage:[self getImageWithClipRect:clipR fromImage:selBigImage] forState:UIControlStateSelected]; [button setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected]; // 监听按钮的点击 [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; // 默认选中第一个 if (i == 0) { [self buttonClick:button]; // button.backgroundColor = [UIColor blueColor]; } } } return self; } /// 布局子控件 - (void)layoutSubviews { [super layoutSubviews]; // 转盘界面 self.backImageView.frame = CGRectMake(0, 0, WHEEL_WIDTH, WHEEL_WIDTH); self.centerImagerView.layer.position = CGPointMake(WHEEL_WIDTH * 0.5, WHEEL_WIDTH * 0.5); self.centerImagerView.bounds = CGRectMake(0, 0, WHEEL_WIDTH, WHEEL_WIDTH); // 开始选号按钮 self.startButton.frame = CGRectMake((WHEEL_WIDTH - WHEEL_WIDTH / 3.5) / 2, (WHEEL_WIDTH - WHEEL_WIDTH / 3.5) / 2, WHEEL_WIDTH / 3.5, WHEEL_WIDTH / 3.5); // 号码按钮 CGFloat btnW = 68; CGFloat btnH = 143; for (int i = 0; i < 12; i++) { QWheelButton *button = self.centerImagerView.subviews[i]; // 按钮尺寸位置 button.bounds = CGRectMake(0, 0, btnW, btnH); button.layer.position = CGPointMake(WHEEL_WIDTH * 0.5, WHEEL_WIDTH * 0.5); button.layer.anchorPoint = CGPointMake(0.5, 1); // 按钮的旋转角度 CGFloat radion = (30 * i) / 180.0 * M_PI; button.transform = CGAffineTransformMakeRotation(radion); } } /// 定时器触发事件处理 - (void)angleChange { // 每一次调用旋转多少 90 / 60.0 CGFloat angle = (90 / 60.0) * M_PI / 180.0; // 持续旋转,需要设置 centerImagerView 的 layer.position 值 self.centerImagerView.transform = CGAffineTransformRotate(self.centerImagerView.transform, angle); } /// 按钮选中点击事件处理 - (void)buttonClick:(UIButton *)button { // 取消之前的按钮选择状态 self.selectedButton.selected = NO; // 设置当前点击的按钮选择状态 button.selected = YES; self.selectedButton = button; } /// 点击开始选号的时候 - (void)startPicker:(UIButton *)button { // 不需要定时器旋转 self.link.paused = YES; // 中间的转盘快速的旋转,并且不需要与用户交互 CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"transform.rotation"; anim.toValue = @(M_PI * 2 * 3); anim.duration = 0.5; anim.delegate = self; [self.centerImagerView.layer addAnimation:anim forKey:nil]; // 根据选中的按钮获取旋转的度数,通过 transform 获取角度 CGFloat angle = atan2(self.selectedButton.transform.b, self.selectedButton.transform.a); // 点击哪个星座,就把当前星座指向中心点上面 self.centerImagerView.transform = CGAffineTransformMakeRotation(-angle); } /// 动画协议方法 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.link.paused = NO; }); } /// 懒加载定时器 - (CADisplayLink *)link { if (_link == nil) { _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(angleChange)]; [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; } return _link; } /// 裁剪图片 - (UIImage *)getImageWithClipRect:(CGRect)clipR fromImage:(UIImage *)image { // 裁剪图片,裁剪区域是以像素为基准 CGImageRef cgImage = CGImageCreateWithImageInRect(image.CGImage, clipR); return [UIImage imageWithCGImage:cgImage]; } @end ViewController.m // 创建转盘界面 QWheelView *wheelView = [QWheelView q_wheelViewWithFrame:CGRectMake(40, 150, 300, 300)]; [self.view addSubview:wheelView]; self.wheelView = wheelView; // 开始转动 [self.wheelView q_startAnimating]; // 停止转动 [self.wheelView q_stopAnimating]; 效果 1.3 折叠图片 一张图片必须要通过两个控件展示,旋转的时候,只旋转一部分控件,通过 layer 控制图片的显示内容,可以让一张完整的图片通过两个控件显示。 @interface ViewController () // 上下两部分图片绘制控件 @property (nonatomic, strong) UIImageView *topImageView; @property (nonatomic, strong) UIImageView *bottomImageView; // 触控控件 @property (nonatomic, strong) UIView *dragView; // 渐变图层 @property (nonatomic, strong) CAGradientLayer *gradientL; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor cyanColor]; // 添加上下两部分图片绘制控件,两个控件叠加在一起,且在位于 drawView 中间 self.bottomImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 250, 200, 100)]; self.bottomImageView.image = [UIImage imageNamed:@"demo"]; [self.view addSubview:self.bottomImageView]; self.topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 250, 200, 100)]; self.topImageView.image = [UIImage imageNamed:@"demo"]; [self.view addSubview:self.topImageView]; // 设置图片显示的尺寸 self.topImageView.layer.contentsRect = CGRectMake(0, 0, 1, 0.5); // 范围 0 ~ 1 self.topImageView.layer.anchorPoint = CGPointMake(0.5, 1); self.bottomImageView.layer.contentsRect = CGRectMake(0, 0.5, 1, 0.5); self.bottomImageView.layer.anchorPoint = CGPointMake(0.5, 0); // 添加渐变图层 self.gradientL = [CAGradientLayer layer]; self.gradientL.frame = self.bottomImageView.bounds; self.gradientL.opacity = 0; // 设置透明度 self.gradientL.colors = @[(id)[UIColor clearColor].CGColor, (id)[UIColor blackColor].CGColor]; // 设置渐变颜色 // self.gradientL.colors = @[(id)[UIColor redColor].CGColor, // (id)[UIColor greenColor].CGColor, // (id)[UIColor yellowColor].CGColor]; // self.gradientL.locations = @[@0.1, @0.4, @0.5]; // 设置渐变定位点 // gradientL.startPoint = CGPointMake(0, 1); // 设置渐变开始点,取值 0~1 [self.bottomImageView.layer addSublayer:self.gradientL]; // 添加触控控件 self.dragView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 200, 200)]; UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [self.dragView addGestureRecognizer:pan]; // 添加手势 [self.view addSubview:self.dragView]; } // 拖动的时候旋转上部分内容 - (void)pan:(UIPanGestureRecognizer *)pan { // 获取偏移量 CGPoint transP = [pan translationInView:self.dragView]; // 旋转角度,往下逆时针旋转 CGFloat angle = -transP.y / 200.0 * M_PI; CATransform3D transfrom = CATransform3DIdentity; transfrom.m34 = -1 / 500.0; // 增加旋转的立体感,近大远小,d:距离图层的距离 transfrom = CATransform3DRotate(transfrom, angle, 1, 0, 0); self.topImageView.layer.transform = transfrom; // 设置阴影效果 self.gradientL.opacity = transP.y * 1 / 200.0; if (pan.state == UIGestureRecognizerStateEnded) { // 反弹 // 弹簧效果的动画,SpringWithDamping:弹性系数,越小弹簧效果越明显 [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{ self.topImageView.layer.transform = CATransform3DIdentity; } completion:nil]; } } @end 效果 1.4 图片倒影 QRepView.h @interface QRepView : UIView @end QRepView.m @implementation QRepView // 设置控件主层的类型 + (Class)layerClass { return [CAReplicatorLayer class]; } @end ViewController.m #import "QRepView.h" @interface ViewController () @property (nonatomic, strong) QRepView *repView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor cyanColor]; self.repView = [[QRepView alloc] initWithFrame:CGRectMake(100, 100, 150, 150)]; [self.view addSubview:self.repView]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.repView.bounds]; imageView.image = [UIImage imageNamed:@"demo.jpg"]; [self.repView addSubview:imageView]; // 创建复制图层 CAReplicatorLayer *repLayer = (CAReplicatorLayer *)self.repView.layer; // 设置子层数量 repLayer.instanceCount = 2; // 往下面平移旋转控件 CATransform3D translation = CATransform3DMakeTranslation(0, self.repView.bounds.size.height, 0); CATransform3D rotateTranslation = CATransform3DRotate(translation, M_PI, 1, 0, 0); repLayer.instanceTransform = rotateTranslation; // 设置阴影 repLayer.instanceAlphaOffset = -0.1; repLayer.instanceRedOffset = -0.1; repLayer.instanceGreenOffset = -0.1; repLayer.instanceBlueOffset = -0.1; } @end 效果 1.5 音量振动条 创建音量振动条 @property (nonatomic, strong) UIView *vibrationBarView; self.vibrationBarView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; self.vibrationBarView.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:self.vibrationBarView]; // 创建复制图层 CAReplicatorLayer *repLayer = [CAReplicatorLayer layer]; // 可以把图层里面所有子层复制 repLayer.frame = self.vibrationBarView.bounds; [self.vibrationBarView.layer addSublayer:repLayer]; // 创建震动条图层 CALayer *layer = [CALayer layer]; layer.anchorPoint = CGPointMake(0.5, 1); layer.position = CGPointMake(30, self.vibrationBarView.bounds.size.height); layer.bounds = CGRectMake(0, 0, 30, 150); layer.backgroundColor = [UIColor whiteColor].CGColor; [repLayer addSublayer:layer]; // 设置缩放动画 CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"transform.scale.y"; anim.toValue = @0.1; anim.duration = 0.5; anim.repeatCount = MAXFLOAT; anim.autoreverses = YES; // 设置动画反转 [layer addAnimation:anim forKey:nil]; // 设置复制层中子层 repLayer.instanceCount = 4; // 设置复制层里面有多少个子层,包括原始层 repLayer.instanceTransform = CATransform3DMakeTranslation(45, 0, 0); // 设置复制子层偏移量,不包括原始层,相对于原始层 x 偏移 repLayer.instanceDelay = 0.1; // 设置复制层动画延迟时间 repLayer.instanceColor = [UIColor greenColor].CGColor; // 设置复制子层背景色 repLayer.instanceGreenOffset = -0.3; 效果 1.6 活动指示器 创建活动指示器 @property (nonatomic, strong) UIView *activityIndicatorView; self.activityIndicatorView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; self.activityIndicatorView.backgroundColor = [UIColor redColor]; [self.view addSubview:self.activityIndicatorView]; // 创建复制图层 CAReplicatorLayer *repLayer = [CAReplicatorLayer layer]; // 可以把图层里面所有子层复制 repLayer.frame = self.activityIndicatorView.bounds; [self.activityIndicatorView.layer addSublayer:repLayer]; // 创建指示器图层 CALayer *layer = [CALayer layer]; layer.transform = CATransform3DMakeScale(0, 0, 0); layer.position = CGPointMake(self.activityIndicatorView.bounds.size.width / 2, 20); layer.bounds = CGRectMake(0, 0, 10, 10); layer.backgroundColor = [UIColor greenColor].CGColor; [repLayer addSublayer:layer]; // 设置缩放动画 CGFloat duration = 1.0; CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"transform.scale"; anim.fromValue = @1; anim.toValue = @0; anim.duration = duration; anim.repeatCount = MAXFLOAT; [layer addAnimation:anim forKey:nil]; // 设置复制层中子层 CGFloat count = 20; CGFloat angle = M_PI * 2 / count; repLayer.instanceCount = count; // 设置复制层里面有多少个子层,包括原始层 repLayer.instanceTransform = CATransform3DMakeRotation(angle, 0, 0, 1); // 设置复制子层偏移量,不包括原始层,相对于原始层 x 偏移 repLayer.instanceDelay = duration / count; // 设置复制层动画延迟时间 效果 1.7 粒子运动效果 QParticleMotionView.h @interface QParticleMotionView : UIView /// 创建粒子运动效果界面 + (instancetype)q_particleMotionViewWithFrame:(CGRect)frame; /// 开始运动 - (void)q_startAnimating; /// 清除运动 - (void)q_clearAnimating; @end QParticleMotionView.m @interface QParticleMotionView () /// 贝塞尔路径 @property (nonatomic, strong) UIBezierPath *path; /// 复制图层 @property (nonatomic, strong) CAReplicatorLayer *repLayer; /// 圆点图层 @property (nonatomic, strong) CALayer *dotLayer; /// 圆点个数 @property (nonatomic, assign) NSInteger dotCount; @end @implementation QParticleMotionView /// 创建粒子运动效果界面 + (instancetype)q_particleMotionViewWithFrame:(CGRect)frame { QParticleMotionView *particleMotionView = [[self alloc] initWithFrame:frame]; particleMotionView.backgroundColor = [UIColor whiteColor]; return particleMotionView; } /// 开始运动 - (void)q_startAnimating { // 创建关键帧动画 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; anim.keyPath = @"position"; anim.path = self.path.CGPath; anim.duration = 3; anim.repeatCount = MAXFLOAT; [self.dotLayer addAnimation:anim forKey:@"dotAnimation"]; // 复制子层 self.repLayer.instanceCount = self.dotCount; self.repLayer.instanceDelay = 0.2; } /// 清除运动 - (void)q_clearAnimating { // 移除动画 [self.dotLayer removeAnimationForKey:@"dotAnimation"]; // 清空路径 self.path = nil; // 清空子层总数 self.dotCount = 0; [self setNeedsDisplay]; } /// 初始化 - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.dotCount = 0; // 创建复制图层 self.repLayer = [CAReplicatorLayer layer]; self.repLayer.frame = self.bounds; [self.layer addSublayer:self.repLayer]; // 创建圆点图层 CGFloat wh = 10; self.dotLayer = [CALayer layer]; self.dotLayer.frame = CGRectMake(0, -1000, wh, wh); self.dotLayer.cornerRadius = wh / 2; self.dotLayer.backgroundColor = [UIColor blueColor].CGColor; [self.repLayer addSublayer:self.dotLayer]; } return self; } /// 触摸开始 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { // 获取触摸起始点位置 CGPoint startPoint = [touches.anyObject locationInView:self]; // 设置起点 [self.path moveToPoint:startPoint]; } /// 触摸移动 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { // 获取触摸点位置 CGPoint touchPoint = [touches.anyObject locationInView:self]; // 添加线到某个点 [self.path addLineToPoint:touchPoint]; // 重绘 [self setNeedsDisplay]; self.dotCount ++; } /// 绘制贝塞尔路径 - (void)drawRect:(CGRect)rect { [self.path stroke]; } /// 懒加载路径 - (UIBezierPath *)path { if (_path == nil) { _path = [UIBezierPath bezierPath]; } return _path; } @end ViewController.m @property (nonatomic, strong) QParticleMotionView *particleMotionView; // 创建粒子运动效果界面 CGRect frame = CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height - 100); self.particleMotionView = [QParticleMotionView q_particleMotionViewWithFrame:frame]; [self.view addSubview:self.particleMotionView]; // 开始运动 [self.particleMotionView q_startAnimating]; // 清除运动 [self.particleMotionView q_clearAnimating]; 效果 1.8 划定路径移动 DrawView.m @interface DrawView () @property (nonatomic, strong) UIBezierPath *path; @end @implementation DrawView /// 触摸开始 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { // 移除上一个动画 [self.subviews.firstObject.layer removeAnimationForKey:@"drawAnimation"]; // 获取触摸起始点位置 CGPoint startPoint = [touches.anyObject locationInView:self]; // 创建路径 self.path = [UIBezierPath bezierPath]; // 设置起点 [self.path moveToPoint:startPoint]; } /// 触摸移动 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { // 获取触摸点位置 CGPoint touchPoint = [touches.anyObject locationInView:self]; [self.path addLineToPoint:touchPoint]; [self setNeedsDisplay]; } /// 触摸结束 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { // 添加关键帧动画 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; anim.keyPath = @"position"; // 添加动画路径 anim.path = self.path.CGPath; anim.duration = 3; anim.repeatCount = MAXFLOAT; anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; // 给子视图添加核心动画 [self.subviews.firstObject.layer addAnimation:anim forKey:@"drawAnimation"]; } /// 触摸取消 - (void)touchesCancelled:(NSSet *)touches withEvent:(nullable UIEvent *)event { [self touchesEnded:touches withEvent:event]; } /// 绘图 - (void)drawRect:(CGRect)rect { [self.path stroke]; } @end ViewController.m DrawView *myDrawView = [[DrawView alloc] initWithFrame:self.view.bounds]; myDrawView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:myDrawView]; UIImageView *imv = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)]; imv.image = [UIImage imageNamed:@"heart2"]; [myDrawView addSubview:imv]; 效果 1.9 粘性效果按钮 QGooButton.h @interface QGooButton : UIButton @end QGooButton.m /// 最大圆心距离 #define MAX_DISTANCE 80 #define WIDTH self.bounds.size.width @interface QGooButton () /// 小圆视图 @property (nonatomic, strong) UIView *smallCircleView; /// 不规则矩形 @property (nonatomic, strong) CAShapeLayer *shapeLayer; /// 小圆原始半径 @property (nonatomic, assign) CGFloat oriSmallRadius; /// 小圆原始中心 @property (nonatomic, assign) CGPoint oriSmallCenter; @end @implementation QGooButton /// 懒加载 - (CAShapeLayer *)shapeLayer { if (_shapeLayer == nil) { // 展示不规则矩形,通过不规则矩形路径生成一个图层 _shapeLayer = [CAShapeLayer layer]; _shapeLayer.fillColor = self.backgroundColor.CGColor; [self.superview.layer insertSublayer:_shapeLayer below:self.layer]; } return _shapeLayer; } - (UIView *)smallCircleView { if (_smallCircleView == nil) { _smallCircleView = [[UIView alloc] init]; _smallCircleView.backgroundColor = self.backgroundColor; // 小圆添加按钮的父控件上 [self.superview insertSubview:_smallCircleView belowSubview:self]; } return _smallCircleView; } /// 初始化 - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 记录小圆最初始半径和中心 self.oriSmallRadius = WIDTH / 2; // 设置按钮圆角 self.layer.cornerRadius = WIDTH / 2; // 设置按钮文字颜色和字体大小 [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; self.titleLabel.font = [UIFont systemFontOfSize:18]; // 添加手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [self addGestureRecognizer:pan]; } return self; } - (void)layoutSubviews { [super layoutSubviews]; static int flag = 1; if (flag) { flag = 0; // 设置小圆位置和尺寸 self.smallCircleView.center = self.center; self.smallCircleView.bounds = self.bounds; self.smallCircleView.layer.cornerRadius = WIDTH / 2; } } /// 拖动手势事件处理 - (void)pan:(UIPanGestureRecognizer *)pan { // 获取手指偏移量 CGPoint transP = [pan translationInView:self]; // 移动控件位置 CGPoint center = self.center; center.x += transP.x; center.y += transP.y; self.center = center; // 复位 [pan setTranslation:CGPointZero inView:self]; // 显示后面圆,后面圆的半径,随着两个圆心的距离不断增加而减小 // 计算圆心距离 CGFloat d = [self circleCenterDistanceWithBigCircleCenter:self.center smallCircleCenter:self.smallCircleView.center]; // 计算小圆的半径 CGFloat smallRadius = self.oriSmallRadius - d / 10; // 设置小圆的尺寸 self.smallCircleView.bounds = CGRectMake(0, 0, smallRadius * 2, smallRadius * 2); self.smallCircleView.layer.cornerRadius = smallRadius; // 当圆心距离大于最大圆心距离 if (d > MAX_DISTANCE) { // 可以拖出来 // 隐藏小圆 self.smallCircleView.hidden = YES; // 移除不规则的矩形 [self.shapeLayer removeFromSuperlayer]; self.shapeLayer = nil; } else if (d > 0 && self.smallCircleView.hidden == NO) { // 有圆心距离,并且圆心距离不大,才需要展示 // 展示不规则矩形,通过不规则矩形路径生成一个图层 self.shapeLayer.path = [self pathWithBigCirCleView:self smallCirCleView:self.smallCircleView].CGPath; } // 手指抬起的时候 if (pan.state == UIGestureRecognizerStateEnded) { // 当圆心距离大于最大圆心距离 if (d > MAX_DISTANCE) { // 展示 gif 动画 UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds]; NSMutableArray *arrM = [NSMutableArray array]; for (int i = 1; i < 9; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",i]]; [arrM addObject:image]; } imageView.animationImages = arrM; imageView.animationRepeatCount = 1; imageView.animationDuration = 0.5; [imageView startAnimating]; [self addSubview:imageView]; self.backgroundColor = [UIColor clearColor]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self removeFromSuperview]; }); } else { // 移除不规则矩形,还原 [self.shapeLayer removeFromSuperlayer]; self.shapeLayer = nil; // 还原位置 [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{ // 设置大圆中心点位置 self.center = self.smallCircleView.center; } completion:^(BOOL finished) { // 显示小圆 self.smallCircleView.hidden = NO; }]; } } } /// 计算两个圆心之间的距离 - (CGFloat)circleCenterDistanceWithBigCircleCenter:(CGPoint)bigCircleCenter smallCircleCenter:(CGPoint)smallCircleCenter { CGFloat offsetX = bigCircleCenter.x - smallCircleCenter.x; CGFloat offsetY = bigCircleCenter.y - smallCircleCenter.y; return sqrt(offsetX * offsetX + offsetY * offsetY); } /// 描述两圆之间一条矩形路径 - (UIBezierPath *)pathWithBigCirCleView:(UIView *)bigCirCleView smallCirCleView:(UIView *)smallCirCleView { CGPoint bigCenter = bigCirCleView.center; CGFloat x2 = bigCenter.x; CGFloat y2 = bigCenter.y; CGFloat r2 = bigCirCleView.bounds.size.width / 2; CGPoint smallCenter = smallCirCleView.center; CGFloat x1 = smallCenter.x; CGFloat y1 = smallCenter.y; CGFloat r1 = smallCirCleView.bounds.size.width / 2; // 获取圆心距离 CGFloat d = [self circleCenterDistanceWithBigCircleCenter:bigCenter smallCircleCenter:smallCenter]; CGFloat sinθ = (x2 - x1) / d; CGFloat cosθ = (y2 - y1) / d; // 坐标系基于父控件 CGPoint pointA = CGPointMake(x1 - r1 * cosθ , y1 + r1 * sinθ); CGPoint pointB = CGPointMake(x1 + r1 * cosθ , y1 - r1 * sinθ); CGPoint pointC = CGPointMake(x2 + r2 * cosθ , y2 - r2 * sinθ); CGPoint pointD = CGPointMake(x2 - r2 * cosθ , y2 + r2 * sinθ); CGPoint pointO = CGPointMake(pointA.x + d / 2 * sinθ , pointA.y + d / 2 * cosθ); CGPoint pointP = CGPointMake(pointB.x + d / 2 * sinθ , pointB.y + d / 2 * cosθ); UIBezierPath *path = [UIBezierPath bezierPath]; // A [path moveToPoint:pointA]; // AB [path addLineToPoint:pointB]; // 绘制 BC 曲线 [path addQuadCurveToPoint:pointC controlPoint:pointP]; // CD [path addLineToPoint:pointD]; // 绘制 DA 曲线 [path addQuadCurveToPoint:pointA controlPoint:pointO]; return path; } @end ViewController.m // 创建粘性按钮 QGooButton *gooButton = [[QGooButton alloc] initWithFrame:CGRectMake(200, 200, 50, 50)]; [gooButton setBackgroundColor:[UIColor redColor]]; [gooButton setTitle:@"10" forState:UIControlStateNormal]; [self.view addSubview:gooButton]; 效果 1.10 启动动画 QWelcomeView.h @interface QWelcomeView : UIView /// 创建欢迎视图 + (instancetype)q_weicomeView; @end QWelcomeView.m @interface QWelcomeView () @property (weak, nonatomic) IBOutlet UIImageView *backView; @property (weak, nonatomic) IBOutlet UIImageView *sloganView; @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *textView; @end @implementation QWelcomeView /// 创建欢迎视图 + (instancetype)q_weicomeView{ return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject; } /// 初始化 - (void)awakeFromNib { [super awakeFromNib]; self.backView.image = [UIImage imageNamed:@"ad_background"]; self.sloganView.image = [UIImage imageNamed:@"compose_slogan"]; self.iconView.image = [UIImage imageNamed:@"QianChia0123"]; self.textView.text = @"欢迎回来"; } /// 已经添加到父视图上 - (void)didMoveToSuperview { [super didMoveToSuperview]; // 设置头像圆角 self.iconView.layer.cornerRadius = 50; self.iconView.layer.masksToBounds = YES; // 头像下移 self.iconView.transform = CGAffineTransformMakeTranslation(0, 50); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration:1.0 animations:^{ // 文字图片慢慢消失 self.sloganView.alpha = 0; } completion:^(BOOL finished) { // 显示头像 self.iconView.hidden = NO; [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{ // 头像往上移动的动画,弹簧效果 self.iconView.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { self.textView.alpha = 0; self.textView.hidden = NO; // 文字慢慢显示 [UIView animateWithDuration:0.5 animations:^{ // 欢迎回来 的文字慢慢显示 self.textView.alpha = 1; } completion:^(BOOL finished) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 移除 [self removeFromSuperview]; }); }]; }]; }]; }); } @end AppDelegate.m #import "QWelcomeView.h" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 创建窗口 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIStoryboard *stroyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; UIViewController *vc = [stroyboard instantiateInitialViewController]; self.window.rootViewController = vc; [self.window makeKeyAndVisible]; // 显示欢迎界面 QWelcomeView *welcomeV = [QWelcomeView q_weicomeView]; welcomeV.frame = self.window.bounds; // 注意:一定要给界面设置 Frame [self.window addSubview:welcomeV]; return YES; } 效果 1.11 菜单按钮动画 QMenuItemModel.h @interface QMenuItemModel : NSObject @property (nonatomic, strong) NSString *title; @property (nonatomic, strong) UIImage *image; + (instancetype)q_menuItemWithTitle:(NSString *)title image:(UIImage *)image; @end QMenuItemModel.m @implementation QMenuItemModel + (instancetype)q_menuItemWithTitle:(NSString *)title image:(UIImage *)image { QMenuItemModel *itme = [[self alloc] init]; itme.title = title; itme.image = image; return itme; } @end QMenuItemButton.h @interface QMenuItemButton : UIButton @end QMenuItemButton.m #define kImageRatio 0.8 @implementation QMenuItemButton - (void)awakeFromNib { [super awakeFromNib]; [self setUp]; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setUp]; } return self; } - (void)setUp { self.imageView.contentMode = UIViewContentModeCenter; self.titleLabel.textAlignment = NSTextAlignmentCenter; [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } /// 布局子控件 - (void)layoutSubviews { [super layoutSubviews]; // UIImageView CGFloat imageX = 0; CGFloat imageY = 0; CGFloat imageW = self.bounds.size.width; CGFloat imageH = self.bounds.size.height * kImageRatio; self.imageView.frame = CGRectMake(imageX, imageY, imageW, imageH); // UILabel CGFloat labelY = imageH; CGFloat labelH = self.bounds.size.height - labelY; self.titleLabel.frame = CGRectMake(imageX, labelY, imageW, labelH); } @end QComposeItemViewController.h @interface QComposeItemViewController : UIViewController @end QComposeItemViewController.m #import "QMenuItemModel.h" #import "QMenuItemButton.h" @interface QComposeItemViewController () @property (nonatomic, strong) NSArray *items; @property (nonatomic, strong) NSTimer *timer; @property (nonatomic, assign) int btnIndex; @property (nonatomic, strong) NSMutableArray *itemButtons; @end @implementation QComposeItemViewController /// - (void)viewDidLoad { [super viewDidLoad]; // 添加所有 item 按钮 [self setUpAllBtns]; // 按钮按顺序的从下往上偏移 self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES]; } /// 添加所有 item 按钮 - (void)setUpAllBtns { int cols = 3; int col = 0; int row = 0; CGFloat x = 0; CGFloat y = 0; CGFloat wh = 100; CGFloat margin = ([UIScreen mainScreen].bounds.size.width - cols * wh) / (cols + 1); CGFloat oriY = 300; for (int i = 0; i < self.items.count; i++) { UIButton *btn = [QMenuItemButton buttonWithType:UIButtonTypeCustom]; col = i % cols; row = i / cols; x = margin + col * (margin + wh); y = row * (margin + wh) + oriY; btn.frame = CGRectMake(x, y, wh, wh); // 设置按钮的图片和文字 QMenuItemModel *item = self.items[i]; [btn setImage:item.image forState:UIControlStateNormal]; [btn setTitle:item.title forState:UIControlStateNormal]; // 偏移到底部 btn.transform = CGAffineTransformMakeTranslation(0, self.view.bounds.size.height); [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown]; [btn addTarget:self action:@selector(btnClick1:) forControlEvents:UIControlEventTouchUpInside]; [self.itemButtons addObject:btn]; [self.view addSubview:btn]; } } /// 给所有的按钮做动画 - (void)setUpAllBtnAnim { for (UIButton *btn in self.itemButtons) { [self setUpOneBtnAnim:btn]; } } /// 给一个按钮做动画 - (void)setUpOneBtnAnim:(UIButton *)btn { [UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseIn animations:^{ btn.transform = CGAffineTransformIdentity; } completion:nil]; } /// 定时器响应事件处理 - (void)timeChange { // 让一个按钮做动画 if (self.btnIndex == self.itemButtons.count) { // 定时器停止 [self.timer invalidate]; return; } // 获取按钮 UIButton *btn = self.itemButtons[self.btnIndex]; [self setUpOneBtnAnim:btn]; self.btnIndex++; } /// 按钮按下响应事件处理 - (void)btnClick:(UIButton *)btn { [UIView animateWithDuration:0.5 animations:^{ btn.transform = CGAffineTransformMakeScale(1.2, 1.2); }]; } /// 按钮点击响应事件处理 - (void)btnClick1:(UIButton *)btn { [UIView animateWithDuration:0.5 animations:^{ btn.transform = CGAffineTransformMakeScale(2.0, 2.0); btn.alpha = 0; }]; NSLog(@"%s", __func__); } /// 懒加载 - (NSArray *)items { if (_items == nil) { // 创建模型对象 QMenuItemModel *item1 = [QMenuItemModel q_menuItemWithTitle:@"点评" image:[UIImage imageNamed:@"tabbar_compose_review"]]; QMenuItemModel *item2 = [QMenuItemModel q_menuItemWithTitle:@"更多" image:[UIImage imageNamed:@"tabbar_compose_more"]]; QMenuItemModel *item3 = [QMenuItemModel q_menuItemWithTitle:@"拍摄" image:[UIImage imageNamed:@"tabbar_compose_camera"]]; QMenuItemModel *item4 = [QMenuItemModel q_menuItemWithTitle:@"相册" image:[UIImage imageNamed:@"tabbar_compose_photo"]]; QMenuItemModel *item5 = [QMenuItemModel q_menuItemWithTitle:@"文字" image:[UIImage imageNamed:@"tabbar_compose_idea"]]; QMenuItemModel *item6 = [QMenuItemModel q_menuItemWithTitle:@"签到" image:[UIImage imageNamed:@"tabbar_compose_review"]]; _items = @[item1, item2, item3, item4, item5, item6]; } return _items; } - (NSMutableArray *)itemButtons { if (_itemButtons == nil) { _itemButtons = [NSMutableArray array]; } return _itemButtons; } @end ViewController.m // 点击加号按钮 - (IBAction)btnClick:(id)sender { QComposeItemViewController *vc = [[QComposeItemViewController alloc] init]; [self presentViewController:vc animated:YES completion:nil]; } 效果 1.12 引导页动画 ViewController.m @interface ViewController () <UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIImageView *sunView; @property (weak, nonatomic) IBOutlet UIImageView *personView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIImage *bgImage = [UIImage imageNamed:@"520_userguid_bg"]; // ScrollView UIScrollView *scorollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; scorollView.contentSize = bgImage.size; scorollView.delegate = self; scorollView.decelerationRate = 0.5; [self.view insertSubview:scorollView atIndex:0]; // bg UIImageView *bgImageView = [[UIImageView alloc] initWithImage:bgImage]; CGRect rect = bgImageView.frame; rect.size.height = self.view.bounds.size.height; bgImageView.frame = rect; [scorollView addSubview:bgImageView]; // cg UIImage *cgImage = [UIImage imageNamed:@"520_userguid_cg"]; UIImageView *cgImageView = [[UIImageView alloc] initWithImage:cgImage]; rect = cgImageView.frame; rect.size.height = self.view.bounds.size.height; cgImageView.frame = rect; [bgImageView addSubview:cgImageView]; // fg UIImage *fgImage = [UIImage imageNamed:@"520_userguid_fg"]; UIImageView *fgImageView = [[UIImageView alloc] initWithImage:fgImage]; rect = cgImageView.frame; rect.size.height = self.view.bounds.size.height; fgImageView.frame = rect; [bgImageView addSubview:fgImageView]; // cloud UIImage *cloudImage = [UIImage imageNamed:@"520_userguid_cloud"]; UIImageView *cloudImageView = [[UIImageView alloc] initWithImage:cloudImage]; [bgImageView addSubview:cloudImageView]; } // 只要一滚动就会调用 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 获取 scrollView 偏移量 CGFloat offsetX = scrollView.contentOffset.x; int intOffsetX = (int)offsetX; // 切换人物的图片 NSString *imageName = [NSString stringWithFormat:@"520_userguid_person_taitou_%d", (intOffsetX % 2 + 1)]; self.personView.image = [UIImage imageNamed:imageName]; // 旋转小太阳 self.sunView.transform = CGAffineTransformRotate(self.sunView.transform, 5 / 180.0 * M_PI); } @end 效果

优秀的个人博客,低调大师

iOS - Mac Apache WebServer 服务器配置

前言 Apache 是目前使用最广的 Web 服务器,可以支持各种脚本的执行。 Mac 系统自带,无需单独安装,只需要修改几个配置就可以,简单,快捷。 有些特殊的服务器功能,Apache 都能很好的支持。例如:HTTP PUT/DELETE 等操作。 1、Apache WebServer 配置 1)准备工作: 为了保证电脑的安全,必须设置用户密码。 2)配置服务器: 1> 配置服务器的工作: 在 Finder 中创建一个 "ApacheWebServer" 的文件夹,直接创建在 /Users/JHQ0228(当前用户名)目录下。 修改配置文件中的 "两个路径",指向刚刚创建的文件夹。 修改一个 Options 配置项。 反注释一个文件路径配置。 拷贝一个文件。 2> 配置服务器注意事项: 关闭中文输入法。 命令和参数之间需要有 "空格"。 修改系统文件一定记住 "sudo",否则会没有权限。 目录要在 /Users/JHQ0228(当前用户名)目录下。 3> 配置服务器: 提示:$ 开头的,可以拷贝,但是不要拷贝 $ 。 "直接在当前用户目录下创建一个 ApacheWebServer 的文件夹,打开终端,按照下面的操作开始配置" # 切换工作目录 $ cd /etc/apache2 # 备份文件,以防不测,只需要执行一次就可以了,格式 cp (copy 的缩写) (源文件) (目标文件) # 如果后续操作出现错误,可以使用以下命令恢复 $ sudo cp httpd.conf.bak httpd.conf $ sudo cp httpd.conf httpd.conf.bak # 用 vim 打开 httpd.conf 文件 $ sudo vim httpd.conf # 查找 DocumentRoot,命令模式下输入 /(查找),区分大小写 * /DocumentRoot # "将光标移动到 DocumentRoot 所在行" DocumentRoot "/Library/WebServer/Documents" <Directory "/Library/WebServer/Documents"> # 进入编辑模式,将光标移动到 DocumentRoot 所在行,直接按键盘上的 i 键 * i # "修改引号中的路径为上面在 Finder 中创建的路径" DocumentRoot "/Users/JHQ0228/ApacheWebServer" <Directory "/Users/JHQ0228/ApacheWebServer"> # "往下滑动找到 Options FollowSymLinks Multiviews 行",Mac 10.10+ 的 Apache 需要修改这一行 Options FollowSymLinks Multiviews # "在 Options 与 FollowSymLinks 之间插入 Indexes" Options Indexes FollowSymLinks Multiviews # 进入命令模式,直接按键盘上的 esc 键 * esc # 查找 php,命令模式下输入 /(查找),区分大小写 * /php # "将光标移动到 #LoadModule php5_module libexec/apache2/libphp5.so 行首" #LoadModule php5_module libexec/apache2/libphp5.so # 删除行首注释 #,命令模式下直接按键盘上的 x 键 * x LoadModule php5_module libexec/apache2/libphp5.so # 保存并退出,命令模式下输入 :wq ,不保存退出为 :q! * :wq # 切换工作目录,etc 目录有点类似于 windows/system32,存放配置文件的目录 $ cd /etc # 拷贝 php.ini 文件 $ sudo cp php.ini.default php.ini # 重新启动 apache 服务器 $ sudo apachectl -k restart # "如果出现以下错误提示,表示配置完成" httpd: Could not reliably determine the server's fully qualified domain name, using JHQ0228-MacBookAir.local. Set the 'ServerName' directive globally to suppress this message # 将服务器脚本文件放到前面设置的 ApacheWebServer 文件夹中。 # 打开浏览器,在浏览器地址栏中输入服务器地址如 http://192.168.88.200 ,能够进入到 “Index of /” 页面。 # 或者输入本地地址 http://localhost 进行测试。 3)常见问题: 1> 如果点击服务器网站资源中的 info.php 文件,出现下载,或者只是显示一小段文字 解决办法: 在终端中输入以下两个命令: $ sudo apachectl -k stop // 关闭 apache 服务器 $ sudo apachectl -k start // 重新再次启动 apache 服务器 2> 每次启动计算机,Apache 服务器默认是不会自动启动的 可以启动计算机之后,打开终端,输入以下命令: $ sudo apachectl -k start // 启动 apache 服务器 设置开机启动: $ sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist 关闭开机启动: $ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 3> 最常见的问题 交换文件已经存在,直接按字母 d,可以删除交换文件。 4> Mac 10.10+ 的 Apache 配置略微有一些不一样 在 httpd.conf 中找到 "Options FollowSymLinks Multiviews" 加一个单词 Indexes,修改后的结果如下: "Options Indexes FollowSymLinks Multiviews" 5> 执行脚本的时候,显示没有权限,拒绝访问。用 NTFS 格式的 U 盘拷贝网络素材,会把文件本身的权限过滤掉。 以下是在终端中修改文件权限的指令: $ ls -la // 查看当前文件夹中的文件访问权限 $ chmod 644 info.php(没有权限的文件名) // 将指定的文件权限修改为 -rw-r--r-- -读写-只读-只读-,644(110 100 100) $ chmod 644 *.* // 将所有的文件权限修改为 -rw-r--r--

优秀的个人博客,低调大师

iOS - Mac OS X 常用快捷键

Mac OS X 常用快捷键 1)快捷键图标: Escape 为 esc 键 无 Eject 键时用电源键代替 Space Bar 为空格键 2)基本快捷键: command + x 剪切文字 command + c 复制 command + v 粘贴 command + z 撤销 command + a 全选 command + s 保存 command + f 查找 3)截图: command + shift + 3 截取全屏到文件 command + shift + 4 截取选中屏幕区域到文件,按空格键仅选中一个窗口 command + shift + control + 3 截取全屏到剪贴板 command + shift + control + 4 截取选中屏幕区域到剪贴板,按空格键仅选中一个窗口 4)系统: control + shift + Eject 锁屏(无 Eject 键时用电源键代替) control + Eject 弹出关机选项,用 tab 或 shift + tab 调整选项,按空格键执行虚框选中内容;按回车键直接关机。 command + option + Eject 睡眠 command + control + Eject 重启,直接重启无提示 command + option + control + Eject 关机,直接关机无提示 command + option + esc 打开强制退出窗口 command + tab 切换当前应用程序 command + h 隐藏当前正在运行的应用程序窗口 command + option + h 隐藏其它应用程序窗口 command + m 最小化当前窗口 command + w 关闭窗口(不一定退出程序) command + q 关闭当前应用程序(退出程序) command + space 输入法切换 command + ? 获得帮助 command + F5 打开/关闭 VoiceOver(朗读菜单、按钮) shift + volume 调节音量并发出声音 option + volume 打开 “声音” 的 “系统设置” option + shift + volume 微调音量(原有的 16 格调节细化到 64 格) tab 或 shift + tab 对话框中切换不同选项,需在“键盘设置”—> “快捷键”中,选中“所有控制”, 按空格执行虚框选中项,按回车执行深颜色选中项。 5)文本处理: command + ⬆️ 将光标移动到文稿开头 command + ⬇️ 将光标移动到文稿末尾 command + ⬅️ 将光标切换到当前行的行首 command + ️ 将光标切换到当前行的行尾 fn + ⬆️ 向上滚动一页(Page Up) fn + ⬇️ 向下滚动一页(Page Down) fn + ⬅️ 滚动至文稿开头(Home) fn + ️ 滚动至文稿末尾(End) command + b 切换文字粗体显示 fn + delete 向后删除(相当于 PC 全尺寸键盘上的 delete 键) command + z 撤销 command + shift + z 重做 6)在 Finder 中: command + shift + n 新建文件夹 command + o 打开选中的文件或文件夹 command + ⬆️ 打开包含此文件的文件夹(相当于 PC 中的 “向上”) command + shift + g 调出 “前往文件夹” 窗口,可输入绝对路径直达文件夹 command + option + v 剪切文件或文件夹,在 A 中对文件复制(command + c), 在 B 中按下此快捷键,即将文件剪切到 B 中。 return 重命名选中的文件或文件夹 command + delete 将文件移至废纸篓 command + shift + delete 清倒废纸篓 7)在浏览器中: command + l 光标直接跳至地址栏 control + tab 转向下一个标签页 control + shift + tab 转向上一个标签页 control + 加号/等号 放大页面 control + 减号 缩小页面 8)窗口缩放: 在 Yosemite 版本中,最大化的 + 按钮已经变为了 全屏按钮,但是通过以下方式依旧可以实现最大化。 1> 按住 Option 全屏按钮就会变为 + , 点击就是垂直最大化; 2> 按住 shift+Opt 全屏按钮也会变为 + , 点击就是最大化,而不是全屏;

优秀的个人博客,低调大师

iOS - Mac 锁屏快捷键设置

Mac 锁屏快捷键设置 control + shift + Eject 锁屏快捷键 如果用户要离开电脑一段时间,可以选择直接把笔记本直接合上。但是这样原先在跑的进程就会挂起或者结束,如果正在下载,那么下载就被暂停(有时还不能恢复),如果正在提供网络服务,那么因为网络断了,别人也连不上你的笔记本。锁屏可以解决这个问题,在 Windows 下用 Win+L 快捷键就锁屏了,但 Mac OS X 下一直没有默认的快捷键。 1)方法一 1> Finder –> 应用程序 –> 实用工具 –> 钥匙串访问,或者点击 Mac 屏幕右上角的放大镜按钮 Spotlight 搜索“钥匙串”; 2> 点击 Mac 顶部菜单栏的 “钥匙串访问”,选择 “偏好设置”; 3> 勾选第一项 “在菜单栏中显示钥匙串状态”; 4> Mac 顶部菜单栏会多出一个锁图标,想锁屏的时候点击选择第一项 “锁定屏幕”。 上述的方法实现了锁屏,但还没能达到快捷键控制锁屏的目标。虽然如此,但它的效果给我们一个锁屏思路。首先,我们要求屏幕保护程序在恢复时必须输入密码,然后锁屏问题就变成启动 屏幕保护程序的问题了。Lock the screen via a keyboard shortcut 这篇文章叙述了具体的实施方法。 第一步,找到 “系统偏好设置” 下的 “安全性与隐私”,在 “通用” 页里勾上 “进入睡眠或开始屏幕保护程序后立即要求输入密码”。 第二步,要用快捷键启动屏幕保护程序,相对复杂一点。在 “应用程序” 里找到 “Automator”。新建一个服务,在 “操作” 下的 “实用工具” 里找到 “启动屏幕保护程序”,并把此操作拖动到右边,并且选择 “没有输入” 和位于 “任何应用程序”,如下图所示。然后把服务保存为自己定义的名字。 第三步:在 “系统偏好设置” 里找到 “键盘”,然后为刚才定义的服务配置快捷键,我设成了 command + control + l 。 2)方法二 1> Finder –> 应用程序 –> 实用工具 –> 钥匙串访问,或者点击 Mac 屏幕右上角的放大镜按钮 Spotlight 搜索“钥匙串”; 2> 点击 Mac 顶部菜单栏的 “钥匙串访问”,选择 “偏好设置”; 3> 勾选第一项 “在菜单栏中显示钥匙串状态”; 4> Mac 顶部菜单栏会多出一个锁图标,想锁屏的时候点击选择第一项 “锁定屏幕”。 第一步:点击左上角苹角 LOGO 找到 系统偏好设置 –> 安全与隐私 –> 通用 –> 把屏保勾起来,选立即; 第二步:桌面与屏幕保护程序 –> 点屏幕保护程度 –> 触发角; 第三步:在点击触发角后 –> 出现活跃的屏幕角 –> 点左下角下拉菜单(任意4角都行) –> 启动屏幕保护程序 –> 好; 第四步:把窗口关了,把鼠标拉到左下角(如活跃角设置其他角,就放到对应角)稍等 1 秒等!就完成锁屏咯。

优秀的个人博客,低调大师

iOS - Mac Apache WebDav 服务器配置

前言 Apache 服务器: Web 服务器,可以支持各种脚本(PHP)的执行,目前世界上使用最为广泛的一种 Web 服务器 WebDav 服务器: 基于 http 协议的 "文件" 服务器 实现文件的上传/下载/修改/删除 WebDav 权限: 授权信息的格式 BASIC (用户名:口令)base64 安全性并不高,密码很容易被拦截和破解。 应用场景:开发企业级的管理系统,可以用 WebDav 搭建一个内部的文件管理服务器,只是在公司内网使用。 FTP 服务器: 文件传输协议,基于 FTP 的一个文件管理服务器 可以做文件的上传/下载/修改/删除 以上三种服务器,只要 ip 地址能够访问,无论在任何位置,都能够使用。 1、Apache WebDav 配置 1)准备工作: 为了保证电脑的安全,必须设置用户密码。 put 配置脚本文件下载 2)配置服务器: 1> 配置服务器的工作: 修改了两个配置文件。 创建 web 访问用户的用户名和口令。 创建了两个目录,并且设置管理权限。 2> 配置服务器注意事项: 关闭中文输入法。 命令和参数之间需要有 "空格"。 修改系统文件一定记住 "sudo",否则会没有权限。 目录要在 /Users/JHQ0228(当前用户名)目录下。 3> 配置服务器: # 切换目录 $ cd /etc/apache2 $ sudo vim httpd.conf # 查找httpd-dav.conf /httpd-dav.conf "删除行首#" # 将光标定位到行首 0 # 删除行首的注释 x # 保存退出 :wq 注意:要在 Mac 10.10+ 配置 Web-dav 还需要在 httpd.conf 中打开以下三个模块 LoadModule dav_module libexec/apache2/mod_dav.so LoadModule dav_fs_module libexec/apache2/mod_dav_fs.so LoadModule auth_digest_module libexec/apache2/mod_auth_digest.so # 切换目录 $ cd /etc/apache2/extra # 备份文件(只要备份一次就行) $ sudo cp httpd-dav.conf httpd-dav.conf.bak # 编辑配置文件 $ sudo vim httpd-dav.conf "将 Digest 修改为 Basic" # 查找Digest /Digest # 进入编辑模式 i # 返回到命令行模式 ESC # 保存退出 :wq # 切换目录,可以使用鼠标拖拽的方式 $ cd 保存 put 脚本的目录 # 以管理员权限运行 put 配置脚本(对于 OS X 10.11 + 用户,需要关闭 SIP 安全设置) $ sudo ./put # 输入系统密码:当前用户密码 # 设置两次 WebDav 密码:adminpasswd(密码随便设置) # 当返回的文件列表中包含有如下信息时即表示配置成功。 drwxr-xr-x 2 _www _www 68 3 30 11:50 uploads -rw-r--r-- 1 root _www 44 3 30 11:50 user.passwd drwxr-xr-x 2 _www _www 68 3 30 11:50 var # 设置的用户名为:admin,密码为:adminpasswd # 点击 Finder 的菜单 前往 => 连结服务器(command + k) # 在弹出的对话框的服务器地址中输入要连结的 WebDav 服务器的 IP 地址。 # 如输入 http://192.168.88.200/uploads 点击连结。 # 或者输入本地回环地址 http://127.0.0.1/uploads 进行测试。 # 验证连结身份时,使用注册用户,名称和密码为前边设置的内容,如名称:admin,密码:adminpasswd # put 配置脚本执行的内容 # 切换目录 $ cd /usr # 设置用户 admin 的密码 $ htpasswd -c /usr/user.passwd admin # 设置密码文件的访问群组 $ chgrp www /usr/user.passwd # 建立 var 文件夹,保存 DavLockDB 相关文件 $ mkdir -p /usr/var # 修改 var 文件夹用户群组 $ chown -R www:www /usr/var # 建立上传文件夹:uploads $ mkdir -p /usr/uploads # 修改 uploads 文件夹用户群组 $ chown -R www:www /usr/uploads # 确认 $ ls -lG # 重新启动 Apache $ apachectl -k restart 3)常见问题: 1> mac root 用户 在 usr 目录下没有写权限 对于 Mac OS X 10.11 + 用户,由于系统启用了 SIP(System Integrity Protection), 导致 root 用户也没有权限修改 /usr 目录。按如下方式可恢复权限。 屏蔽方法: 重启 Mac,按住 command + R,进入 recovery 模式。选择打开 Utilities 下的终端, 输入:csrutil disable 并回车,然后正常重启 Mac 即可。 如果想想重新开启该安全设置,重复上面步骤,在终端中输入的命令更改为 csrutil enable。

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码