Giter Club home page Giter Club logo

ios-multipletimers's Introduction

iOS-MultipleTimers

TabView Timer 구현

TabView에 여러 개의 타이머를 생성하고 원하는 Timer를 삭제할 수 있다.


실행화면

Timer 구현

먼저 Timer구현은 Timer.publish를 사용

 Timer.publish(every: 1, on: .main, in: .common)
            .autoconnect()
            .receive(on: DispatchQueue.main)
            .sink { _ in
                print("Call Timer Publish")
            }

생성 시 , 1초마다 sink의 클로저를 실행시킨다.

Model 추가

여러개의 Timer가 동작되야하기 떄문에 Array로 Timer를 관리하는게 좋을 것 같았다. Timer만 따로 관리하면 추적이 어려울 것 같아 Model을 만들어 하나로 관리하기 위해 Model을 만들었다.

import Foundation
import Combine

struct TimerItem: Identifiable {
    var id: UUID
    var count: Int
    var subscriptions: AnyCancellable?
}

Model 설명을 하자면, id는 각각의 Timer별로 고유아이디, count는 Timer를 이용해 값을 이용한 변수, subscriptions Timer이다. 초기엔 idNumber라는 변수를 두어 index로 구별했었지만, 중복된 정보인것같아, id로 각각의 Timer에 대해 접근한다.

TabView 구현

  • Array
 TabView(selection: $currentIndex) {
     ForEach(viewModel.timerItemStore, id: \.id) { item in
            VStack{
                HStack {
                    Text("ID: \(item.id)")
                        .lineLimit(2)
                        .font(.footnote)
                        .fontWeight(.semibold)
                    
                    Spacer()
                }
                Spacer()
                
                Text("Count: \(item.count)")
                    .font(.title3)
                    .fontWeight(.bold)
                
                Spacer()
            }
            .padding()
            .foregroundColor(.white)
        }
    }
    .tabViewStyle(.page)
  • index
TabView(selection: $currentIndex) {
    ForEach(0..<viewModel.timerItemStore.count, id: \.self) { index in
        VStack{
            HStack {
                Text("ID: \(viewModel.timerItemStore[index].id)")
                    .lineLimit(2)
                    .font(.footnote)
                    .fontWeight(.semibold)
                
                Spacer()
            }
            Spacer()
            
            Text("Count: \(viewModel.timerItemStore[index].count)")
                .font(.title3)
                .fontWeight(.bold)
            
            Spacer()
        }
        .padding()
        .foregroundColor(.white)
    }
}
.tabViewStyle(.page)

영상을 보면 array Timer가 실행될떄마다(1초) TabView section이 0 즉 앞으로 계속 이동되고 index로 구현 시 section의 이동이 일어나지 않는다. 원인을 생각해보니, array로 구현 시 해당 데이터의 변화가 생기는 것이기에 다시 View를 그리기 떄문에 생기는 현상으로 생각된다.

array index

ios-multipletimers's People

Contributors

bradheo65 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.