0
我需要實現一個動畫,當從滾動視圖中刪除對象時動作。刪除對象時填充空白區域動畫?
例如,在滾動視圖中有5個對象(視圖)。當對象的開啓被刪除時,另一個將與動畫一起進入其他對象。那就是當第三個被刪除時,第四個和第五個將進入第二個旁邊,它將看起來像是包含四個對象的滾動視圖。
我該如何達到這個要求?
有什麼想法?
self.frame = CGRectMake(0, 0, 1024, 768);
UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
deleteButton.frame = CGRectMake(0, 0, 100, 100);
[deleteButton setTitle:@"fuck" forState:UIControlStateNormal];
[deleteButton addTarget:self action:@selector(removeViews) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:deleteButton];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 200, 800, 100)];
[scrollView setScrollEnabled:YES];
scrollView.pagingEnabled = YES;
[scrollView setDelegate:self];
scrollView.backgroundColor = [UIColor brownColor];
[scrollView setShowsHorizontalScrollIndicator:YES];
[scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.contentMode = UIViewContentModeScaleToFill;
[scrollView setContentSize:CGSizeMake(1000,100)];
[self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]];
int countViews = 6;
//for each number add one circleview to your scrollview. for each circleview add a button, to remove the view from the scrollview
for(int i=0;i<countViews;i++){
UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(100+(i*100),5,90,90)];
circleView.backgroundColor = [UIColor whiteColor];
circleView.tag = i;
[scrollView addSubview:circleView];
UILabel* circleIndex = [[UILabel alloc] init];
circleIndex.frame = CGRectMake(30, 25, 40, 40);
[circleIndex setFont:[UIFont fontWithName:@"Arial-BoldMT" size:40]];
[circleIndex setText:[NSString stringWithFormat:@"%d",i]];
[circleView addSubview:circleIndex];
UIView *exitView = [[UIView alloc] initWithFrame:CGRectMake(70,-5,30,30)];
exitView.layer.cornerRadius = 15;
exitView.backgroundColor = [UIColor redColor];
exitView.layer.borderColor = [[UIColor whiteColor] CGColor];
exitView.layer.borderWidth = 2;
exitView.tag = i;
[exitView setHidden:YES];
[circleView addSubview:exitView];
UIView *exitLabel = [[UIView alloc] initWithFrame:CGRectMake(8,13,15,3)];
exitLabel.backgroundColor = [UIColor clearColor];
exitLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
exitLabel.layer.borderWidth = 2;
[exitView addSubview:exitLabel];
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(handleLongPress:)];
[circleView addGestureRecognizer:longPress];
UITapGestureRecognizer *singlePress =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSinglePress:)];
[exitView addGestureRecognizer:singlePress];
}
//[circleView bringSubviewToFront:exitView];
[self addSubview:scrollView];
}
return self;
}
-(void)removeViews:(id)sender{
[[scrollView viewWithTag:[sender tag]] removeFromSuperview];
[[scrollView viewWithTag:([sender tag]-100)] removeFromSuperview];
//[scrollView setNeedsDisplay];
//[self setNeedsDisplay];
NSLog(@"%d",scrollView.subviews.count);
for(int i=0; i<(scrollView.subviews.count/2-[sender tag]+1);i++)
{
[UIView animateWithDuration:0.5f
delay:0.0f
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[[scrollView viewWithTag:([sender tag]+i)] setFrame:CGRectMake(100+(i*100),5,90,90)]; // last position
}
completion:nil];
}
}
}
//The event handling method
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if (recognizer.state == UIGestureRecognizerStateEnded) {
[self shakeView:recognizer.view];
[[recognizer.view viewWithTag:2] setHidden:NO];
}
}
- (void)handleSinglePress:(UITapGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if (recognizer.state == UIGestureRecognizerStateEnded) {
[self removeViews:[recognizer.view viewWithTag:4]];
}
}
- (void)singlePress:(UITapGestureRecognizer *)recognizer{
//CGPoint location = [recognizer locationInView:[recognizer.view superview]];
if (recognizer.state == UIGestureRecognizerStateEnded) {
[recognizer.view.layer removeAllAnimations];
//[exitView setHidden:YES];
}
}
我試過,但沒有效果 – erdemgc
你還可以設置這個 –
我做到了,仍然沒有效果的幀。另一個有趣的一點是,只有滾動視圖的第一個視圖可以刪除,其他人不能如此 – erdemgc