netflix_Netflix播放按鈕剖析

netflix

We will develop a play pause button similar to the one the Netflix video player has.

我們將開發一個類似于Netflix視頻播放器的播放暫停按鈕。

Since Swift has replaced Objective-C as the default development language for iOS, the same will apply to SwiftUI and UIKit soon enough. Therefore we will be using SwiftUI to develop our button.

由于Swift已將Objective-C替換為iOS的默認開發語言,因此同樣很快將適用于SwiftUI和UIKit。 因此,我們將使用SwiftUI開發按鈕。

PlayButton.swift (PlayButton.swift)

Create a new Xcode project, using the iOS Single View Application template. Let’s call it PlayButtonDemo. In the next screen, make sure to select SwiftUI in the drop down menu titled User Interface.

使用iOS Single View Application模板創建一個新的Xcode項目。 我們稱之為PlayButtonDemo 。 在下一個屏幕中,確保在標題為用戶界面的下拉菜單中選擇SwiftUI

Xcode’s showing options for your new project screen.

Create a new User Interface file, select the SwiftUI View option. Call it PlayButton.

創建一個新的用戶界面文件,選擇SwiftUI View選項。 稱之為PlayButton

Xcode new file template with SwiftUI view selected.

In PlayButton.swift add an action property to the PlayButton struct, that takes a closure as its value. This will be the action the button performs when tapped.

PlayButton.swift中,action屬性添加到PlayButton結構中,該屬性將閉包作為其值。 這是輕按按鈕時按鈕執行的操作

var action: () -> Void

PlayButton_Preview will throw a "Missing Argument…" error. Fix it by supplying the action argument. We will set a simple action that prints Hello World!.

PlayButton_Preview將引發“ Missing Argument…”錯誤。 通過提供action參數對其進行修復。 我們將設置一個簡單的動作來打印Hello World!

struct PlayButton_Previews: PreviewProvider {
static var previews: some View {
PlayButton {
print("Hello Button")
}
}
}

At the end of PlayButton, create a new Shape struct, called PlayPauseShape.

PlayButton的結尾,創建一個新的Shape結構,稱為PlayPauseShape

The above will create a rectangular shape using the supplied rect parameter for dimensions.Back in PlayButton, change the default Text("Hello, World!") to PlayPauseShape(). Our Canvas will look like this.

上面將使用提供的rect參數創建尺寸的rectPlayButton ,將默認Text("Hello, World!")更改為PlayPauseShape() 。 我們的畫布看起來像這樣。

SwiftUI’s canvas showing the PlayPauseShape.

We clearly don’t intend to use a button that big. Let’s set a more appropriate size for our button.

我們顯然不打算使用那么大的按鈕。 讓我們為按鈕設置一個更合適的大小。

In PlayButton_Previews add a frame modifier to PlayButton.

PlayButton_Previews添加一個frame修飾符PlayButton

PlayButton {
print("Hello World!")
}
.frame(width: 128, height: 128)
SwiftUI Canvas with frame modifier set on PlayButton.

Before we start creating the shape, let’s complete PlayButton's construction, by making the button accessible and adding a tap gesture recognizer to it.

在開始創建形狀之前,讓按鈕可訪問并向其添加輕擊手勢識別器,以完成PlayButton的構建。

With regards to accessibility, when video is playing on device, the Pause button is what a user will see. Likewise, when video is paused, the user will see the Play button. Hence VoiceOver will report exactly what the button looks like.The .isButton accessibility trait, informs VoiceOver that it should report this UI element as a button. And finally, when the user double taps the element while it's in focus, VoiceOver will perform performTap(), the same function that our tap gesture recognizer calls.performTap() toggles the button's internal state, from pause to play and vice verca, and then calls the action that is passed in when setting up the button in its parent view.The .contentShape(Rectangle) modifier informs SwiftUI that the button's content has a rectangular shape, making the whole area tappable. Without this modifier, SwiftUI will mask the button's tappable hit area to the shape of our play/pause shape.With our type complete, let us turn our attention to the shape.

關于可訪問性,當在設備上播放視頻時,用戶將看到“ 暫停”按鈕。 同樣,當視頻暫停時,用戶將看到“ 播放”按鈕。 因此,VoiceOver將準確報告按鈕的外觀.isButton可訪問性特征通知VoiceOver它應將此UI元素報告為按鈕。 最后,當用戶在焦點對準的位置上雙擊元素時,VoiceOver將執行performTap() ,這與我們的點擊手勢識別器調用的功能相同。 performTap()可以將按鈕的內部狀態從暫停狀態切換為播放狀態,然后從反之亦然,然后調用在其父視圖中設置按鈕時傳遞的動作。.contentShape .contentShape(Rectangle)修飾符通知SwiftUI該按鈕的內容包含矩形,使整個區域都可以輕敲。 如果沒有此修飾符, SwiftUI將把按鈕的可點擊擊中區域遮蓋為播放/暫停形狀的形狀。完成鍵入后,讓我們將注意力轉移到形狀上。

PlayPauseShape (PlayPauseShape)

We want to split a triangle in half, we want each half to morph into a rectangle. The first challange would be to find the point D in the image below, the intersection of the triangle's top edge with the line running across its centre.

我們想將三角形??分成兩半,我們希望每個半都變形成矩形。 第一個挑戰是在下圖中找到點D ,即三角形的上邊緣與穿過其中心的線的交點。

Triangle with a line in the middle.

It’s actually quite simple, all we need to do is, find the equation of the line using two points A (0, 0) and B (width, height * 0.5).First we find the slope M,M = (By - Ay)/(Bx - Ax) => M = (height * 0.5)/widthWe know that D = (width * 0.5, Dy)Substitue D with either A or B into the slope equation will get us the following:Dy = M(Dx - Ax) + Ay => Dy = (height * 0.5)/width * (width * 0.5)With regards to E, since the play shape is an equilateral triangle (the angles inside the triangle are all the same)Ey = height - Dy.

這其實很簡單,我們需要做的是,找到使用兩個點A(該直線的方程00 )和B( widthheight * 0.5 )。首先,我們發現斜率M,M =(B Y形 - Y)/(B X - X)=> M =( height * 0.5 )/ width我們知道,d =( width * 0.5 ,d y)的與AB成坡方程式Substitue d將得到我們D y = M ( D x - A x )+ A y => D y =( height * 0.5 )/ width *( width * 0.5 )關于E ,由于游戲形狀是等邊三角形 (角度三角形內部都相同) E y = height -D y

We will use a Path to draw the two halves, each half will be a subpath of the main path, and now that we have solved for all the unknowns (width and height are supplied to us by Shape's path(in:)) the first subpath will have the following points (A, D, E, C) and our second subpath will have (D, B, B, E). The duplicate B is not an error, we are animating from a triangle to a rectangular shape, it's better to just collpase the point onto it's self when it's a triangle.With regards to the pause shape, we provide each subpath with a rectangle that is offset from the center of the shape.

我們將使用Path繪制兩半,每一半將成為主路徑的子路徑,現在我們已經解決了所有未知數(Shape的path(in:)提供了widthheight ),第一個子路徑將具有以下點( ADEC ),而我們的第二個子路徑將具有( DBBE )。 重復的B并不是錯誤,我們正在從三角形到矩形進行動畫制作,最好是將點變成三角形時將其自身折疊起來。關于暫停形狀,我們為每個子路徑提供一個矩形從形狀中心偏移。

Pause shape with a line in the middle.

In UIKit, animating between two paths is very easy using CABasicAnimation. In SwiftUI in not as straight forward. We need to provide a parameter to SwiftUI for interpolation. So if we wanted to animate from 0 to 1, SwiftUI will provide us with (0.0, 0.1, 0.2 ... 1.0). We inform SwiftUI of the parameter by implementing the animateableData property on types that conform to Animateable. Hence that parameter needs to be part of the path for the animation to take effect.

UIKit中 ,使用CABasicAnimation非常容易在兩條路徑之間進行動畫處理。 在SwiftUI中并不是那么簡單。 我們需要為SwiftUI提供參數進行插值。 因此,如果我們想從0到1進行動畫處理,SwiftUI將為我們提供(0.0,0.1,0.2 ... 1.0)。 我們通過在符合Animateable類型上實現animateableData屬性來告知SwiftUI參數。 因此,該參數必須是動畫生效路徑的一部分。

For a great write on SwiftUI animation, checkout out this post.

要獲得有關SwiftUI動畫的出色文章,請查看這篇文章 。

We’ll go through how to animate from the play shape’s A point to pause shape’s A point. We define the two points leftPlayTopLeftand leftPauseTopLeft. We find out how far the point has to move from pause's top left to play's top left. Mutliplying the result by the interpolation value and adding to leftPauseTopLeft will animate between to the two shapes.

我們將介紹如何從游戲形狀的A點進行動畫,以暫停形狀的A點。 我們定義了兩點leftPlayTopLeftleftPauseTopLeft 。 我們發現該點必須從暫停的左上角移動到游戲的左上角。 將結果與插值值leftPauseTopLeft并添加到leftPauseTopLeft將在這兩個形狀之間進行動畫處理。

Back to PlayButton.swift, we will now define all 8 points as described above in a function called pathPoints. The value that is being interpolated her is the shift property.

返回PlayButton.swift ,我們現在將在上述名為pathPoints的函數中定義所有8個點。 她要插值的值是shift屬性。

pathPoints(width:height) returns an array that's holding two arrays. The first array contains all the points for the left subpath, while the second array provides the points for the right subpath.We update path(in:) function to loop through the arrays and draw the lines.

pathPoints(width:height)返回一個包含兩個數組的數組。 第一個數組包含左子路徑的所有點,第二個數組提供右子路徑的點。我們更新path(in:)函數以遍歷數組并繪制線。

Click the Live Preview button in the canvas and tap away, you should get the button behaving as below.

單擊畫布中的“ 實時預覽”按鈕,然后輕按一下,您將獲得如下所示的按鈕。

Play button transitioning into pause button.

As always you can download the completed project from github.com.

與往常一樣,您可以從github.com下載完成的項目。

Thank you for reading.

感謝您的閱讀。

Image for post
UX Para Minas Pretas (UX For Black Women), a Brazilian organization focused on promoting equity of Black women in the tech industry through initiatives of action, empowerment, and knowledge sharing. Silence against systemic racism is not an option. Build the design community you believe in.UX Para Minas Pretas (UX For Black Women),這是一個巴西組織,致力于通過采取行動,賦權和知識共享的舉措來促進科技行業中的黑人女性平等。 對系統性種族主義保持沉默是不可行的。 建立您相信的設計社區。

翻譯自: https://uxdesign.cc/anatomy-of-the-netflix-play-button-d45cf0eb18c6

netflix

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274762.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274762.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274762.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

TypeScript 終極初學者指南

大家好,我是若川。持續組織了8個月源碼共讀活動,感興趣的可以點此加我微信 ruochuan12 參與,每周大家一起學習200行左右的源碼,共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列在過去…

繼承與多態(六)

繼承 1.繼承 a。.直接在類的后面加上冒號“:”后面跟基類,就該類就繼承了基類的一切特性了。 b。private類不能被繼承,只有public、protected類能被繼承。 c。private類不里面所有的屬性和方法都不能被外界訪問,只有他自己可以。 …

標記偏見_如何(巧妙地)扭曲視覺效果以支持您的偏見敘事

標記偏見Data is important — it is the logical justification for world-changing decisions. Unfortunately, arrays of numbers don’t tell as interpretable a story as a picture does, providing an insatiable need for data visualizations.數據很重要-這是改變世界…

高瓴投資,頂配創業團隊,dora 誠招前端 / Flutter

dora 是一個可以跨越設計稿,直接生成應用的新一代設計工具。讓任何 Creator 都能輕松構建個性化的網站和應用,無需編寫一行代碼。通過自主研發的全新技術,我們為用戶打造了完全自由度的設計與開發體驗,足以滿足任何復雜場景的個性…

獵鷹spacex_SpaceX:簡單,美觀的界面是未來

獵鷹spacex重點 (Top highlight)A photo has been floating around the internet showing how the interior of the new Dragon spacecraft differs from the Space Shuttle. The difference is staggering, but not entirely suprprising. After all the Shuttle started oper…

object的classid收集

比如&#xff1a; wbbrowser控件 <OBJECT idWB classidCLSID:8856F961-340A-11D0-A96B-00C04FD705A2 VIEWASTEXT></OBJECT> 畫圖控件 <OBJECT idSGrfxCtl1 classidclsid:369303C2-D7AC-11D0-89D5-00A0C90833E6 ></OBJECT> 上下滾動條控件 <OB…

如何高效學習前端新知識,拓展視野,我推薦

技術日新月異&#xff0c;發展迅速&#xff0c;作為一個與時俱進的互聯網人&#xff0c;需要不斷地學習擴寬視野。今天為大家推薦幾個技術領域中出類拔萃的公眾號&#xff0c;它們的每一篇推文都值得你點開&#xff01;1前端開發愛好者學習路線 數據結構算法 前端進階「前端開發…

開發交接文檔_為開發人員創造更好的設計交接體驗

開發交接文檔It’s 2020. We’re supposed to have flying cars and space travel. We should at least have our process for design handoff nailed down at this point.現在是2020年。我們應該有飛行汽車和太空旅行。 在這一點上&#xff0c;我們至少應該確定我們的設計移交…

同步器之Exchanger

類java.util.concurrent.Exchanger提供了一個同步點&#xff0c;在這個同步點&#xff0c;一對線程可以交換數據。每個線程通過exchange()方法的入口提供數據給他的伙伴線程&#xff0c;并接收他的伙伴線程提供的數據&#xff0c;并返回。 當在運行不對稱的活動時很有用&#x…

?Cookie 從入門到進階:一文徹底弄懂其原理以及應用

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列Cooki…

ui設計師常用的設計工具_2020年應該使用哪個UI設計工具?

ui設計師常用的設計工具重點 (Top highlight)It’s 2020, the market today is saturated with UI design tools. Ever since Sketch app came out with its sleek, simple, and efficient tool to craft user interface design, many companies have followed suit to take a …

Ajax拖放頁面元素(圖片)

最近了解了一點YUI的控件知識.先做個Ajax拖放頁面元素(圖片)以便學習參考. 現在有一些網站如QQ空間,都允許用戶自定義模塊,可以任意拖動模塊到各個地方去.YUI在這一方面做得比較好.下面以一組圖片的方式來說明如何運用Ajax拖放頁面元素: 第一步:在<head></head>標簽…

你不知道的vscode之空間控制

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列王志遠…

正則表達式說明

參考地址&#xff1a; 正則表達式說明 正則表達式全部符號解釋轉載于:https://www.cnblogs.com/s-bridge/archive/2012/06/26/2564396.html

lynda ux_UX心態

lynda uxI have had the pleasure of training and mentoring several UX people at the beginning of their careers.在職業生涯的初期&#xff0c;我很高興接受培訓和指導。 Whatever your background or experience, I’ve found repeatedly that there are some key miles…

什么 Leader 值得追隨?

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。目前建有江西|湖南|湖北 籍 前端群&#xff0c;想進群的可以加我微信 ruochuan12 進群。歷…

pico8 掌機_使用Pico-8構建自己的復古游戲

pico8 掌機An example of the kinds of pixel animations people make in Pico-8.人們在Pico-8中制作的各種像素動畫的示例。 Are you a fan of old school video games? What if I told you there’s an NES-style game devkit with the sound/sprite/code tools all built i…

C#中Brush、Color、String相互轉換

1、String轉換成Color Color color (Color)ColorConverter.ConvertFromString(string); 2、String轉換成Brush BrushConverter brushConverter new BrushConverter(); Brush brush (Brush)brushConverter.ConvertFromString(string); 3、Color轉換成Brush Brush …

實用 JavaScript 調試技巧

大家好&#xff0c;我是若川。持續組織了8個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

數據挖掘 點擊更多 界面_6(更多)技巧,可快速改善用戶界面

數據挖掘 點擊更多 界面重點 (Top highlight)Creating beautiful, usable, and efficient UIs takes time, with many design revisions along the way.創建漂亮&#xff0c;可用和高效的UI需要花費時間&#xff0c;并且在此過程中進行了許多設計修訂。 Making those constant…