2011-11-20 2 views
2

내 고객/날짜에 따라 MKMapView에서 발견되는 다양한 장소 표시에 대해 불투명도 (알파)를 설정하도록 요청 받았습니다.날짜에 따른 알파 설정

장소 표시의 가장 오래된 절반 인 경우 불투명도를 0.5로 설정하고 싶습니다.

장소 표시의 색인을 만들고 배열의 마지막 부분에 있는지 확인하여이를 수행 할 수 있습니다.

int indexOfPlacemark = [fixes indexOfObject:fix]; //fixes is the array of Placemarks (named fix). 

if (index <= [fixes count]/2) { 
    [annotationView setAlpha:0.5]; 
} 

else { 
    // do something with fix.date to work out the opacity. 
    // an example of the date is Sun, May 15, 2011 - 12:00:44 
} 

하지만 그는 그 날짜에 따라 다른 모든 장소 표시에 대해 불투명도를 높이기를 원합니다. 즉, 최신 날짜는 불투명도가 1.0이고 배열의 전반부에서 가장 오래된 날짜는 ~ 0.5가됩니다.

내가 알고 싶은 것은 날짜에 따라 불투명도를 해결하는 방법입니다. 고급의

감사합니다.

XcodeDev

답변

2

먼저 가장 오래된 날짜와 최신 날짜 사이에 경과 된 시간 얻어야한다 :

NSTimeInterval timeElapsed = [oldestDate timeIntervalSinceDate:fix.date]; 
[annotationView setAlpha:1 - ((maxTimeElapsed - timeElapsed)/maxTimeElapsed)/2]; 
: 각 장소 표시 아이콘에 대한

NSTimeInterval maxTimeElapsed = [latestDate timeIntervalSinceDate:oldestDate]; 

그런를, 당신은 다음을 수행해야합니다

이렇게하면 이전 날짜와 함께 timeElapsed가 0에 가까워 질수록 alphas는 0.5에 가까워집니다. 그리고 더 새로운 날짜로, timeElapsed는 maxTimeElapsed에 더 가깝고, alpha를 1에 가깝게 만든다.

2

[fixes count]/2에서 불투명도가 0.5이고 개체가 [fixes count] -1에서 불투명도가 1.0이고 그 사이의 값이 균등하게 분산되면 단지 다음과 같이 할 수 있습니다. :

[annotationView setAlpha:(float)indexOfPlacemark/[fixes count]]; 

각 객체에 부착 된 NSDate을했습니다 그 기반으로 계산을 할 경우 먼저 같은 날짜의 범위를 얻을 수 있습니다 :

// set up 
NSDate *startDate = [[fixes objectAtIndex:[fixes count]/2] date]; 
NSDate *endDate = [[fixes lastObject] date]; 
NSTimeInterval range = [endDate timeIntervalSinceDate:startDate]; 

// ... 
// and later, for each object beyond halfway through the list: 
NSTimeInterval timeSinceHalfway = [[fix date] timeIntervalSinceDate:startDate]; 
[annotationView setAlpha: 0.5 + (timeSinceHalfway/range) * 0.5]; 

산술 상대적으로이를 간단한 - 당신이 cur를 나눈다면 객체의 시간을 전체 범위만큼 임대하면 0과 1 사이의 숫자를 얻습니다. 범위의 시작에 맞으면 0, 끝 부분에 있으면 1입니다. 0.5를 곱하면 0과 0.5 사이의 숫자가됩니다. 0.5와 1 사이의 숫자를 얻기 위해 0.5를 더할 수 있습니다.

관련 문제