首页 iOS.& Swift Books 通过教程的iOS开奖结果3d

17
中风& Path Animations 由Marin Todorov撰写

在本章中,您将了解笔划和路径开奖结果3d,因为您在应用程序假装从Internet获取新数据时,您将很酷的Pack-to-Refresh开奖结果3d添加到现有的Pack List项目,请为您的现有包列表项目添加到现有的Pack List项目

一路上,您将学习如何为形状的绘图进行开奖结果3d,作为奖励,您将查看特殊类型的关键帧开奖结果3d,您可以使用沿任何任意路径移动对象。

创建交互式笔划开奖结果3d

为本章打开Starter项目,然后构建并运行它以查看UI的外观:

存在现有的代码 ViewController.swift. 为您填充桌面,为您提供多个休假项目。拉下表,您将看到屏幕顶部显示刷新视图:

刷新视图保持四秒钟可见,然后缩回。您的工作是在他们等待时添加一个有趣的开奖结果3d来娱乐用户。

刷新视图已包含拉动和释放操作的所有代码;您只需要担心添加开奖结果3d。

笔记:下拉刷新代码基于我们的视频教程之一。如果您想了解更多关于它的工作方式检查 Swift滚动视图学校 视频系列下列链接: //videos.raywenderlich.com.

建立开奖结果3d的第一步是创建一个圆形形状。打开 refreshview.swift. and add the following code to init(frame:scrollView:):

ovalShapeLayer.strokeColor = UIColor.white.cgColor
ovalShapeLayer.fillColor = UIColor.clear.cgColor
ovalShapeLayer.lineWidth = 4.0
ovalShapeLayer.lineDashPattern = [2, 3]

let refreshRadius = frame.size.height/2 * 0.8

ovalShapeLayer.path = UIBezierPath(ovalIn: CGRect(
  x: frame.size.width/2 - refreshRadius,
  y: frame.size.height/2 - refreshRadius,
  width: 2 * refreshRadius,
  height: 2 * refreshRadius)
).cgPath

layer.addSublayer(ovalShapeLayer)

ovalShapeLayer is a property on RefreshView of type CAShapeLayer. You’re already quite familiar with shape layers; here you simply set the stroke and fill colors and set the circle diameter to be 80% of the view height, which ensures a comfortable margin around the shape.

There’s one property in the code above that you haven’t encountered yet: lineDashPattern. This property lets you set a dash pattern for the shape stroke; you simply you provide an array with the length of the dash and the length of the gap in pixels.

构建并运行您的项目,看看圈子的外观:

这看起来非常好 - 很容易创造。这将为您提供圆形进度条。

In RefreshView, redrawFromProgress() is called whenever the user scrolls via scrollViewDidScroll(_ scrollView:); this makes it a convenient place to update the visuals of the progress bar.

Add the following code to redrawFromProgress():

ovalShapeLayer.strokeEnd = progress

As progress increases from 0.0 to 1.0, the stroke end of the shape moves forward towards the starting point of the stroke.

This is how the shape looks when progress is 0.25:

Here it is halfway around, at 0.5:

建立并运行您的项目;向上和向下拖动表视图以查看笔划长度更改。

现在,您将添加一个很酷的飞机到刷新控制。

Scroll back to init(frame:scrollView:) and add the following code to the bottom of the initializer:

let airplaneImage = UIImage(named: "airplane.png.")!
airplaneLayer.contents = airplaneImage.cgImage
airplaneLayer.bounds = CGRect(x: 0.0, y: 0.0,   
  width: airplaneImage.size.width,   
  height: airplaneImage.size.height)

airplaneLayer.position = CGPoint(  
  x: frame.size.width/2 + frame.size.height/2 * 0.8,   
  y: frame.size.height/2)

layer.addSublayer(airplaneLayer)

代码应该看起来很熟悉。你已经在以前的章节中完成了几次。你只是加载 airplane.png. 并将其分配为屏幕上的图层的内容,然后将飞机层定位在圆圈开始绘制的位置。

在将表视图拉下来时,构建并运行项目以查看飞机显示:

当用户拉下表时,该飞机应淡入。

Add the following code to init(frame:scrollView:):

airplaneLayer.opacity = 0.0

这使得飞机完全透明地开始。

Now add the following code to redrawFromProgress() to progressively change the opacity of the airplane layer as the user pulls down:

airplaneLayer.opacity = Float(progress)

opacity is of type Float so you need to convert progress from a CGFloat.

建立并运行您的项目;拉下桌子,你应该看到飞机逐渐出现:

这包装了本章的交互式开奖结果3d部分。下一节通过开奖结果3d进度指示器来播放,以使用户在等待FAUX刷新数据时啮合。

开奖结果3d两个中风结束

在本节中,您将开奖结果3d 两个都 the strokeStart and strokeEnd properties to make the shape “run around”.

let strokeStartAnimation = CABasicAnimation(
  keyPath: "strokeStart")
strokeStartAnimation.fromValue = -0.5
strokeStartAnimation.toValue = 1.0
  
let strokeEndAnimation = CABasicAnimation(
  keyPath: "strokeEnd")
strokeEndAnimation.fromValue = 0.0
strokeEndAnimation.toValue = 1.0
let strokeAnimationGroup = CAAnimationGroup()
strokeAnimationGroup.duration = 1.5
strokeAnimationGroup.repeatDuration = 5.0
strokeAnimationGroup.animations = 
  [strokeStartAnimation, strokeEndAnimation]
ovalShapeLayer.add(strokeAnimationGroup, forKey: nil)

创建路径密钥框架开奖结果3d

You saw how to animate a layer using a keyframe animation and the values property in Chapter 12, “Keyframe Animations and Struct Properties”. to animate a layer along a path you do much the same thing but you assign a CGPath to the animation’s path property instead.

let flightAnimation = CAKeyframeAnimation(keyPath: "position")
flightAnimation.path = ovalShapeLayer.path
flightAnimation.calculationMode = .paced
let flightAnimationGroup = CAAnimationGroup()
flightAnimationGroup.duration = 1.5
flightAnimationGroup.repeatDuration = 5.0
flightAnimationGroup.animations = [flightAnimation]
airplaneLayer.add(flightAnimationGroup, forKey: nil)

let airplaneOrientationAnimation = CABasicAnimation(keyPath: 
"transform.rotation")
airplaneOrientationAnimation.fromValue = 0
airplaneOrientationAnimation.toValue = 2.0 * .pi
flightAnimationGroup.animations = [flightAnimation,
  airplaneOrientationAnimation]

关键点

  • 您可以为非常流程设置开奖结果3d 绘画 a shape on screen by animating the strokeStart and strokeEnd properties of a CAShapeLayer.
  • You can animate a layer along a path on screen by using a keyframe animation and animating the layer’s position property with a given CGPath.

有一个技术问题?想报告一个错误吗? 您可以向官方书籍论坛中的书籍作者提出问题和报告错误 这里.

有反馈分享在线阅读体验吗? 如果您有关于UI,UX,突出显示或我们在线阅读器的其他功能的反馈,您可以将其发送到设计团队,其中表格如下所示:

© 2021 Razeware LLC

您可以免费读取,本章的部分显示为 混淆了 文本。解锁这本书,以及我们整个书籍和视频目录,带有Raywenderlich.com的专业订阅。

现在解锁

要突出或记笔记,您需要在订阅中拥有这本书或自行购买。