2014-10-02 2 views
5

가로 UIScrollView를 사용하고 있으며 내용 오프셋의 x 값에 따라 배경색을 전환하고 싶습니다.내용 오프셋에 따라 UIScrollview 애니메이션

예 : UIScrollView의 너비는 640px입니다. 콘텐츠 오프셋이 0 픽셀 인 경우 배경색은 빨간색이어야합니다. 콘텐츠 오프셋이 320 픽셀 인 경우 배경은 노란색이어야합니다. 하지만 가장 중요한 부분은 UIScrollview가 0px와 320px 사이에있을 때 배경색이 빨간색과 노란색 사이 여야한다는 것입니다.

힌트 : 검색에서 왼쪽으로 스 와이프하면 iOS 용 트위터 앱에 동일한 애니메이션이 적용됩니다. 네비게이션의 레이블이 약간 사라집니다. 당신의있는 ScrollView 위임에

답변

12

당신은 오프셋 비율에 따라 색상을 만들어야합니다.

이와 같이 색상 사이에 쉬프트를 만드는 가장 쉬운 방법은 HSB 색상 공간을 사용하는 것입니다.

또한 애니메이션이 아닙니다. "애니메이션"효과는 스크롤 뷰에서 사용자에게 제공됩니다. 스크롤보기가 변경 될 때마다 색상을 설정하면됩니다.

위임 메서드에서 이와 비슷한 작업을 수행 할 수 있습니다.

편집 내가 RGB 방식이 중간 값에 수행 방법을 모르는

// this just calculates the percentages now and passes it off to another method. 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    // vertical 
    CGFloat maximumVerticalOffset = scrollView.contentSize.height - CGRectGetHeight(scrollView.frame); 
    CGFloat currentVerticalOffset = scrollView.contentOffset.y; 

    // horizontal 
    CGFloat maximumHorizontalOffset = scrollView.contentSize.width - CGRectGetWidth(scrollView.frame); 
    CGFloat currentHorizontalOffset = scrollView.contentOffset.x; 

    // percentages 
    CGFloat percentageHorizontalOffset = currentHorizontalOffset/maximumHorizontalOffset; 
    CGFloat percentageVerticalOffset = currentVerticalOffset/maximumVerticalOffset; 

    [self scrollView:scrollView didScrollToPercentageOffset:CGPointMake(percentageHorizontalOffset, percentageVerticalOffset)]; 
} 

// this just gets the percentage offset. 
// 0,0 = no scroll 
// 1,1 = maximum scroll 
- (void)scrollView:(UIScrollView *)scrollView didScrollToPercentageOffset:(CGPoint)percentageOffset 
{ 
    UIColor *HSBColor = [self HSBColorForOffsetPercentage:percentageOffset.x]; 

    UIColor *RGBColor = [self RGBColorForOffsetPercentage:percentageOffset.x]; 
} 

// HSB color just using Hue 
- (UIColor *)HSBColorForOffsetPercentage:(CGFloat)percentage 
{ 
    CGFloat minColorHue = 0.0; 
    CGFloat maxColorHue = 0.2; // this is a guess for the yellow hue. 

    CGFloat actualHue = (maxColorHue - minColorHue) * percentage + minColorHue; 

    // change these values to get the colours you want. 
    // I find reducing the saturation to 0.8 ish gives nicer colours. 
    return [UIColor colorWithHue:actualHue saturation:1.0 brightness:1.0 alpha:1.0]; 
} 

// RGB color using all R, G, B values 
- (UIColor *)RGBColorForOffsetPercentage:(CGFloat)percentage 
{ 
    // RGB 1, 0, 0 = red 
    CGFloat minColorRed = 1.0; 
    CGFloat minColorGreen = 0.0; 
    CGFloat minColorBlue = 0.0; 

    // RGB 1, 1, 0 = yellow 
    CGFloat maxColorRed = 1.0; 
    CGFloat maxColorGreen = 1.0; 
    CGFloat maxColorBlue = 0.0; 

    // if you have specific beginning and end RGB values then set these to min and max respectively. 
    // it should even work if the min value is greater than the max value. 

    CGFloat actualRed = (maxColorRed - minColorRed) * percentage + minColorRed; 
    CGFloat actualGreen = (maxColorGreen - minColorGreen) * percentage + minColorGreen; 
    CGFloat actualBlue = (maxColorBlue - minColorBlue) * percentage + minColorBlue; 

    return [UIColor colorWithRed:actualRed green:actualGreen blue:actualBlue alpha:1.0]; 
} 

더 유연하게합니다. 중간에 갈색이 생길 수도 있지만 ... 놀 수 있습니다.

ANYTHING을 제어하는 ​​방법으로 스크롤보기를 사용하는 방법에 대한 아이디어를 얻을 수 있습니다.

불투명도, 크기, 회전, 글꼴 크기 등을 제어 할 수있는이 방법을 사용하면 여러 가지를 결합 할 수도 있습니다 (예 : RGB와 동일).

+1

감사합니다. 완벽하게 작동합니다! – Berendschot

+0

이것이 RGB 색상에서 작동 할 가능성은? – Berendschot

+1

@ Maarten1909 그래,하지만 RGB로 변하는 색상을 만드는 것이 훨씬 어렵다. 내가 그것을 초로 바꿔 줄게. – Fogmeister

-3

은 다음과 같이 뭔가를 시도 :

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
    CGPoint a = self.mainScrollView.contentOffset; 
    if(a.x >= 320){ 
     [UIView animateWithDuration:0.5 animations:^{ 
      [self.view setBackgroundColor:[UIColor redColor]]; 
     }]; 
    } 
    else{ 
     [UIView animateWithDuration:0.5 animations:^{ 
      [self.view setBackgroundColor:[UIColor yellowColor]]; 
     }]; 
    } 
} 
+0

왜 downvote? 위의 코드가 질문에 표현 된 특정 요구 사항에 정확히 맞지 않더라도 질문자가 자신의 색 논리를 작동 시키려고 시도 할 수 있음을 암시합니다. –

+3

내 투표가 아닙니다. 하지만 ...이 질문을하는 사람이 찾고있는 효과를 생성하지 않습니다. scrollView가 319 오프셋에 있으면 노란색으로 애니메이션이 적용됩니다. 그것은 거의 완전히 붉은 색이어야합니다. 또한 이것은 그가 찾고있는 붉은 색과 노란색 효과 사이의 절반을 제공하지 않습니다. 게다가 애니메이션도 여기서는 불필요합니다. – Fogmeister

관련 문제