2017-03-13 3 views
0

그래서 어디서나 확인 했으므로이 것을 파악하기가 어렵습니다. 나는 RedPotion을 사용하여 RubyMotion에 응용 프로그램을 작성하고 있지만, 여기서는 Ruby를 사용하여 필요한 도움을 받는다. 응용 프로그램은 꽤 많은 책 응용 프로그램이므로 텍스트를 올바르게 저장하는 방법을 알아 내려고합니다. 기본적으로 한 줄당 문자 수가 정해지고 화면에서 사용할 수있는 줄 수는 17 줄뿐입니다. 일단 그 내용이 채워지면 나머지 텍스트가 같은 방법으로 사용하기 위해 저장되어 페이지가 뒤집을 때 화면에 다음 세트가 나타나기를 바랍니다. 그런 다음 사용자가 다시 스 와이프하면 해당 텍스트가 뒤로 이동합니다. 배열, 해시를 시도했습니다. 다른 방법. 이 한 가지 문제에 대해 약 3 주 동안 미쳐 버렸습니다. 누구나 루비 방법을 사용하거나 작업을 조정할 수 있습니까?누군가가 텍스트를 재활용하는 방법을 이해할 수 있습니까?

def on_load 
@texts = [ 
      "In the beginning of God's creation of the heavens and the   earth.\nNow the earth was 
      astonishingly empty, and darkness was on the face of the deep, and the spirit of God was 
      hovering over the face of the water.\nAnd God said, 'Let there be light,' and there was light. 
      \nAnd God saw the light that it was good, and God separated between the light and between the darkness. 
      \nAnd God called the light day, and the darkness He called night, and it was evening and it was morning, 
      one day.\nAnd God said, 'Let there be an expanse in the midst of the water, and let it be a separation 
      between water and water.'" 
      ] 


    @recycle = [ @texts[ 0 ] ] 

    @page_idx = 0 


    @header = append!(UIImageView, :header) 
    @text_view = append!(UITextView, :text_view) 
    text_view(@texts[ 0 ]) 

    @text_view.on(:swipe_right) do |sender| 
    recycle_text_forward(@recycle[ -1 ]) 
    end 

    @text_view.on(:swipe_left) do |sender| 
    recycle_text_back(@recycle[ @page_idx ]) 
    end 
end 

def text_view(text) 
    page_words = text.split(' ') 

    number_of_lines_idx = 17 
    each_line_chars = 27 
    chars = "" 
    setter = "" 
    idx = 0 
    all_idx = page_words.length - 1 

    @text_view.text = "" 

    while number_of_lines_idx != 0 
    until chars.length >= each_line_chars 
    break if page_words[ idx ] == nil 
    chars << page_words[ idx ] + " " 
    chars << "\n" if chars.length >= each_line_chars 
    idx += 1 
    end 

    break if page_words[ idx ] == nil 

    number_of_lines_idx -= 1 

    if number_of_lines_idx == 0 
    @recycle << page_words[ idx..all_idx ] 
    @page_idx = @recycle.length - 2 
    end 

    setter = chars 
    chars = "" 
    @text_view.text += setter 
    end 
end 

def recycle_text_forward(text) 
text_view(text.join(' ')) 
end 

def recycle_text_back(text) 
text_view(text) 
end 

답변

1

는 내가 제대로 질문을 이해 잘 모르겠지만, 여기에 내가 제안 할 수있는 것입니다 :

내가 믿는
input = "In the beginning of God's creation..." 

_, lines = input.split(/\s+/).each_with_object(["", []]) do |word, (line, lines)| 
    if line.length + word.length + 1 <= 27 
    line << " " << word 
    else 
    lines << line.dup 
    line.replace(word) 
    end 
end #⇒ Here we got an array of lines, now split by screen size: 

lines.each_slice(17).map { |*lines| lines.join(' ').strip } 
#⇒ ["In the beginning...", "and it was evening and"] 

, 이것은 더 미세 조정을위한 좋은 시작이 될 것입니다.

+0

와우! 모든 것에 정말 감사합니다. 나는 네 답을 너무 감사하게 생각하고 얼마나 빨리 말할 수는 없다. 이것은 특히 재 형식화에 많은 도움이됩니다. 남은 유일한 것은 데이터를 저장하는 것입니다. 이 17 줄은 전체 텍스트의 일부일뿐입니다. 따라서 사용자가 스 와이프 할 때 나머지 텍스트를 저장할 수 있어야 다음 17 줄을 얻을 수 있습니다. 그러나 사용자가 오른쪽으로 스 와이프하면 이전 17 줄로 돌아 가야합니다. 내가 좀 더 이해하기를 바란다. –

+0

마지막 줄의 결과에 이미 저장되어 있습니다. _screens_ 배열을 생성합니다. 직접 해보십시오. – mudasobwa

+0

와우! 나는 네가 똑똑했으면 좋겠어. 정말 그 축복을 의미합니다. 정말 고맙습니다!!!!!! –

관련 문제