1 단계 : 코드의 조각을 실행 매 초마다
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(dispatchSomeRaindrops) userInfo:nil repeats:YES];
2 단계 : 화면을 아래로 보내, 일부 입자를 만들고 그들이 바닥에 도달하면 그들을 청소합니다.
- (void)dispatchSomeRaindrops
{
for (int i = 0; i < 5; i++) {
UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"safari.png"]];
CGFloat halfHeight = view.frame.size.height/2;
CGFloat x = arc4random() % (int)self.view.frame.size.width;
view.center = CGPointMake(x, -halfHeight);
[self.view addSubview:view];
NSTimeInterval duration = 10 + arc4random() % 10;
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
float endY = self.view.frame.size.height + halfHeight;
view.center = CGPointMake(x, endY);
} completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}
}
감사합니다. 내가 지금하고있는 일은 NSMutableArray에 모든 새로운 UIImage 객체를 넣은 다음 화면에서 사라진 객체를 어떻게 든 지우는 것입니다. 그런 식으로 NSMutableArray의 각 요소를 동시에 업데이트 할 수도 있습니다. – user1202422
하나의 배열에서 모든 '맵 객체'(너무 쉽게 게임 용어 사용)를 쉽게 관리 할 수 있습니다. '-makeObjectsPerformSelector :'가 도움이됩니다. –
각 입자의 '높이'(각 프레임)를 확인하고 하단에 도달 한 입자를 삭제/재사용 할 수 있습니다. 각 프레임에 많은'alloc/init'과'dealloc' 대신에 재사용을 강력하게 추천합니다. 그러나 UIImage는 최근에 사용한 이미지로 새로운 인스턴스를 생성 할 때 텍스쳐 (이미지) 데이터를 캐쉬하기 때문에 그렇게해서는 안됩니다. –