UIScrollView是UIView的子类,用于滚动视图的显示,以及当前视图显示不了所有内容时的滚动显示,图片浏览功能也是建立在UIScrollView滚动视图的基础上。PageControl一般与UIScrollView一起使用,用于显示图片浏览动画时具体是第几张图片。
UIScrollViewDelegate的协议:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView; // any offset changes
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // any zoom scale changes
// called on start of dragging (may require some time and or distance to move)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; // called on finger up as we are moving
1.自定义绘图(Quartz2D)(Android中view的绘制onDraw()方法绘制图形):
Quartz2D是iOS中绘制图形的强大框架,可以使用Apple提供Quartz2D API来绘制路径/描影/设置透明度/反锯齿等,Quartz2D的强大之处在于它可以借助图形硬件的功能绘制出精美的图片。
Graphics Context(图形上下文)是一个数据类型(CGContextRef),用于封装Quartz的绘制图像到输出设备的信息,也就是如在输出设备上绘制图形,就必须获取当前的图形上下文。
绘制的工作全部是UIView中进行的。
1.1 绘制线条:
绘制线条的步骤:
- 获取图形上下文;
- 设置起点;
- 设置终点;
- 渲染连线。
-(void)drawRect:(CGRect)rect
{
//获取图形上下文
CGContextRef contextrRef = UIGraphicsGetCurrentContext();
//设置线型
CGContextSetLineWidth(contextrRef, 10);
//设置颜色
CGContextSetRGBStrokeColor(contextrRef, 255, 0, 0, 1);
//设置线条起点
CGContextMoveToPoint(contextrRef, 20, 20);
//设置线条终点
CGContextAddLineToPoint(contextrRef, 240, 240);
//渲染线条
CGContextStrokePath(contextrRef);
}
1.2 绘制矩形:
绘制步骤:
- 通过连接固定点来绘制;
- 指定起点和宽度/高度绘制;
- 填充实心矩形(没有空心绘制方法);
- 设置线条粗细,绘制斜矩形。
//获取图形上下文
CGContextRef contextrRef = UIGraphicsGetCurrentContext();
//指定矩形起点和高度
CGContextAddRect(contextrRef, CGRectMake(20, 20, 200, 200));
//设置颜色
CGContextSetRGBStrokeColor(contextrRef, 255, 0, 0, 1);
//渲染线条
CGContextStrokePath(contextrRef);
1.3 绘制圆形:
-(void)drawRect:(CGRect)rect
{
//获取图形上下文
CGContextRef contextrRef = UIGraphicsGetCurrentContext();
//创建path路径
CGMutablePathRef pathRef = CGPathCreateMutable();
CGContextSetRGBStrokeColor(contextrRef, 255, 0, 0, 1);
//绘制圆形
CGPathAddEllipseInRect(pathRef, NULL, CGRectMake(10, 20, 100, 120));
//将圆形添加到图形上下文中
CGContextAddPath(contextrRef, pathRef);
//渲染线条
CGContextStrokePath(contextrRef);
}
2.iOS动画(Android中帧动画(Frame Animation)和补间动画(Tweened Animation)和属性动画):
iOS实现动画效果有以下三种:
- UIView动画(类Android中的view动画):主要是改变UIView的属性来实现动画的效果;
- CATransition动画(类Android中xml实现的动画那种类型):系统为用户定制了几种用于页面之间切换的动画效果,直接调用即可。它是QuartzCore.framework框架的基础上实现的动画效果,使用之前把QuartzCore.framework框架导入工程中;
- CoreAnimation动画(类Android中属性动画):CoreAnimation动画是iOS动画的核心,也是QuartzCore.framework框架的基础上实现的动画效果,结合CoreGraphic.framework框架的绘图功能能实现更精致的效果。
2.1 UIView动画:
[UIView beginAnimations:<#(nullable NSString *)#> context:<#(nullable void *)#>];
UIView动画中提供两种代理方法:
+ (void)setAnimationWillStartSelector:(nullable SEL)selector; // default = NULL. -animationWillStart:(NSString *)animationID context:(void *)context
+ (void)setAnimationDidStopSelector:(nullable SEL)selector; // default = NULL. -animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
2.2 CATransition动画:
CALayer类即成与NSObject类,相对于UIView而言并没有继承UIResponder类,所有CALayer不能和UIView那样处理响应用户事件。
CALayer本质上一块包含一幅位图(bitmap)的缓冲区。
每个view中带有一个layer层。