Start with this article and introduce each part of LNDanmakuMaster one by one
This article is preceded by: LNDanmakuMaster
What is a LNDanmakuAttributes
- Before creating this framework, I researched some of the popular bullet barrage frameworks on GitHub; Some open source libraries define a bullet-screen as an inheritable Cell or View, through which users can define their own bullet-screen View. However, I think it is not the main job of a bullet-screen framework to specify the type of View for users. Speaking of which, I think the famous advertising slogan of 100 Year Mountain comes to mind: We don’t produce water, we’re just nature’s porters. In fact, I think a reasonable ammunition frame is just like a porter, who only needs to “transport” the ammunition, and does not care about what is inside the ammunition, nor how the things inside come from.
- So LNDanmakuMaster uses Attributes:NSObject as the container for the barrage view; Yes, the Attributes have a UICollectionViewAttributes similar responsibilities, with the biggest difference is that extra DanmakuAttributes contains the time information; This is essential, because the reload, busy state and progress control in this framework are all dependent on timestamp calculation; In this framework, we completely ignore the speed as a spatial variable, and let time control everything, and we’ll talk about the benefits of using time control when we talk about orbits.
- We define an LNDanmakuAttributes as follows: Is a minimal barrage unit that holds View/Layer, aliveTime, Model, size/ Position /Transation, which inherits from NSObject. If a barrage Model + show this barrage of View state information + barrage Considered to be a complete barrage, the Attributes is a box to put these together (seen in this way and UICollectionViewAttributes some different).
LNDanmakuAttributes properties
- Spatial attributes: attributes is often and eventually appear on the screen the barrage of view related properties, these properties can be modified, when playing the barrage, different tracks according to the time property “percent” selectively dynamically adjust the spatial properties, allowing different orbit show different trajectory and the motion form; So these properties are consistent with UIView properties of the same name.
attribute | The property name | role |
---|---|---|
Barrage size | CGSize size | The size of the barrage |
Barrage position | CGPoint position | Barrage position (this position is the left midpoint of the barrage view) |
Barrage transform | CATransform3D transform | The current transformation of each barrage, used for rotation, scaling, etc |
Barrage transparency | Float opacity | Barrage transparency |
- Time attribute: The time attribute usually identifies the whole life cycle of a bullet screen. When the live time of the bullet screen reaches the predetermined total life time, the bullet screen will be removed and the play of the bullet screen is completed. The result of these changes is currentPercent (currentTime/TotalTime). Each track specifies how a projectile should behave at this time, so adjust these times constantly. It means constantly adjusting the form of the barrage, thus generating the animation of the barrage.
attribute | The property name | role |
---|---|---|
Current survival time | NSTimeInterval currentAliveTime | The time the barrage has been on the screen, updated from 0 to totalAliveTime |
Barrage time | NSTimeInterval danmakuTime | The animation time of the barrage itself |
Track of time | NSTimeInterval trackTime | The animation time of the barrage on the track |
The total time | NSTimeInterval totalAliveTime | Total projectile live time: danmakuTime + trackTime |
Broadcast pace | Float currentPercent | The barrage of broadcast pace: currentAliveTime/totalAliveTime |
-
Why is there such a clear distinction between danmakuTime and trackTime? According to different orbits, these times represent different meanings. For the moment, I will give three examples to give a general understanding of the meanings of these two kinds of time: 1. For this kind of track, danmakuTime = 0 and trackTime = 5s should be configured. Because the bullet screen appears on the screen all at once, the time of full display on the track should be emphasized. 2. The track of pop animation should be set with danmakuTime = 0.3s and trackTime = 4.7s, because the bullet screen needs to experience a pop animation time before it stays stable and stays on the screen; 3. DanmakuTime = (danmakuWith/screenWidth) * trackTime should be set as a relationship for the common horizontal sliding track, because the danmakuTime needs to go through its own time from the screen boundary before fully appearing on the screen. So the ratio of the length of the barrage to the width of the screen is exactly the ratio of the time of the barrage to the time of the orbit. So that’s a rough explanation of barrage time and orbital time; This separate definition method is currently I think the most universal time division method.
-
Other attributes
attribute | The property name | role |
---|---|---|
The business model | Id customObj | An optional bullet-style business Model that maintains a correspondence between Attributes and Model, allowing users to quickly find Attributes’ corresponding business Model in various callbacks and respond to it |
Barrage Layer | CALayer * _Nullable presentLayer | Display the layers used for the barrage. You give any Layer you have laid out to represent the barrage on the screen. In general, I recommend using a factory to continuously produce Attributes and layers for the business model, and hang the business model and Layer together on the Attributes. (The custom Layer requires implicit animation to be removed, and the framework itself provides the processed Layer/TextLayer for direct use) |
Barrage View | UIView * _Nullable presentView | Display the View used by this barrage. Like the Layer property, the framework will preferentially use the Layer instead of the View when there is a Layer, and the View will only take effect when the Layer is empty. |
Conclusion: The meaning of using Attributes
-
Attributes pulls the view the user needs to display out of the bullet-screen frame, and the user can even pass it in to Layer to display it.
-
The overhead of NSObject is less than UIView, and using Attributes without a view requires less than a Cell of memory when calculating strategies for a barrage or waiting in a queue.
-
Attributes can maintain the mapping relationship between customModel and customView, and indirectly decouple Model and View, so that users’ different bullet-screen views can be reused to any bullet-screen Model, and multiple views can be produced for the same bullet-screen Model.
-
Maintain time schedule and barrage status.
-
A unified interaction format abstracted between tracks, distributors, and players.