Tranquil App Tech Doc – Game 02 – Touch for Music
Table of Contents

Touch for Music - Programming Documentation

I. Overview

"Touch for Music" is an interactive music game where users control music volume by touching and holding anywhere on the screen. A dynamic pulsing circle provides visual feedback synchronized with the audio changes. The game focuses on intuitive touch interaction and a minimalist aesthetic.

II. Architecture and Components

  • Audio System:
    • AudioManager: Manages audio session configuration, playback, and smooth volume transitions.
  • Visual Feedback:
    • Custom circle animation system using SwiftUI animations.
  • User Interaction:
    • DragGesture: Detects and tracks touch location and state (began, changed, ended).
  • State Management: Manages touch state, circle visibility, and inactivity timeout.

III. Core Functionality

A. Circle Animation

The circle animation provides visual feedback synchronized with the audio volume.

  • Implementation: Uses SwiftUI's @State properties and animations:
struct TouchForMusic: View {
    @State private var circleScale: CGFloat = 1.0
    @State private var circleOpacity: Double = 0.0
    @State private var touchLocation: CGPoint?
    @State private var isTouching: Bool = false // Tracks touch state

    // Example animation:
    .scaleEffect(isTouching ? 1.2 : 1.0) // Scale up when touching
    .opacity(isTouching ? 1.0 : 0.5) // Change opacity based on touch state
    .animation(.easeInOut(duration: 0.2)) // Smooth animation
    // ...
}
  • Key Functionality:
    • Touch-Responsive Scaling and Opacity: The circle scales up slightly and becomes more opaque when the user touches the screen, providing immediate feedback.
    • Smooth Transitions: easeInOut animations are used for smooth transitions between states.
    • Position Tracking: The circle's position is updated to follow the touch location using the touchLocation state.

B. Touch Handling and Volume Control

The DragGesture is used to handle touch input and control the volume.

  • Gesture Handling:

    • The onChanged handler of the DragGesture updates the touchLocation state and calculates the volume based on the vertical touch position.
    • The onEnded handler resets the touchLocation and performs any necessary cleanup.
  • Volume Mapping:

    • The vertical touch position is mapped to a volume range (e.g., 0.0 to 1.0). Touching higher on the screen increases the volume, while touching lower decreases it.
    • A scaling function is used to ensure a smooth and proportional volume change.
// Example volume calculation:
let screenHeight = UIScreen.main.bounds.height
let touchY = touchLocation?.y ?? screenHeight / 2 // Default to center if no touch

let volume = 1.0 - (touchY / screenHeight) // Invert so higher touch = higher volume
audioManager.setVolume(Float(volume)) // Assuming AudioManager has a setVolume function

C. Inactivity Monitoring

A timer is used to detect user inactivity.

  • Implementation: A Timer is scheduled to fire periodically (e.g., every 1 second).
  • Timeout: If no touch events are detected within a predefined timeout period (e.g., 10 seconds), the home button is displayed.
  • Reset: Any touch event resets the inactivity timer.

IV. UI Elements

  • Pulsing Circle: The primary visual element, providing feedback on touch interaction and volume changes.
  • Home Button: Appears after inactivity, allowing users to navigate back to the main menu.
  • Initial Instructions: Brief instructions on how to use the game.

V. Lifecycle Methods

  • onAppear:
    • Configures the audio session.
    • Starts background music playback.
    • Starts inactivity monitoring.
  • onChanged (DragGesture):
    • Updates touchLocation.
    • Calculates and sets the audio volume.
    • Updates circle animation.
    • Resets inactivity timer.
  • onEnded (DragGesture):
    • Resets touchLocation and any relevant states.
  • onDisappear:
    • Fades out audio.
    • Invalidates timers.
    • Cleans up resources.

VI. State Variables Examples

  • circleScale: The current scale of the circle.
  • circleOpacity: The current opacity of the circle.
  • touchLocation: The current touch location.
  • isTouching: A boolean indicating whether the user is currently touching the screen.
  • inactivityTimer: The timer used for inactivity monitoring.
No Comments

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Previous
Next