实现了凤凰新闻的频道订阅界面和功能,点击某个频道,可以将其移入或者移出订阅栏,剩下的频道自动重新排列。
_viewArr1 = [[NSMutableArray alloc] init];
_viewArr2 = [[NSMutableArray alloc] init];
for (int i = 0; i < _modelArr1.count; i++) {
_touchView = [[TouchView alloc]initWithFrame:CGRectMake(KTableStartPointX + KButtonWidth * (i%5), KTableStartPointY + KButtonHeight * (i/5), KButtonWidth, KButtonHeight)];
[_touchView setBackgroundColor:[UIColor colorWithRed:210/255.0 green:210/255.0 blue:210/255.0 alpha:1.0]];
_touchView.label.text = _modelArr1[i];
[_myView addSubview:_touchView];
[_viewArr1 addObject:_touchView];
_touchView->_array = _viewArr1;
if (i == 0) {
[_touchView.label setTextColor:[UIColor colorWithRed:187/255.0 green:1/255.0 blue:1/255.0 alpha:1.0]];
}
else{
[_touchView.label setTextColor:[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1.0]];
}
[_touchView setMoreChannelsLabel:_titleLabel2];
_touchView->_viewArr11 = _viewArr1;
_touchView->_viewArr22 = _viewArr2;
}
2、在_touchView上添加手势,手指一点击离开视图就执行的方法。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if (![self.label.text isEqualToString:@"头条"]) {
if (_array == _viewArr11) {
[_viewArr11 removeObject:self];
[_viewArr22 insertObject:self atIndex:_viewArr22.count];
_array = _viewArr22;
[self animationAction];
}
else if ( _array == _viewArr22){
[_viewArr22 removeObject:self];
[_viewArr11 insertObject:self atIndex:_viewArr11.count];
_array = _viewArr11;
[self animationAction];
}
}
}
在这个方法中主要是两个可变数组的删除和插入。_array == _viewArr11 这里比较的是两个数组中的元素完全相同,在这之前创建_touchView的时候,
曾将 _touchView->_array = _viewArr1;
_touchView->_viewArr11 = _viewArr1;
_touchView->_viewArr22 = _viewArr2;
这些数组传递了过来
注意的是:
它是每创建一个_touchView的时候就传递了一次,也就是说每个_touchView中都是不同的数组,这样才可以
判断出你点击 的_touchView是哪一个,也可以正确的判断出哪个是你删除或增加的_touchView。
3、完成动画效果,也就是将你删除或增加的_touchView后面的视图进行重新布局,
- (void)animationAction{
for (int i = 0; i < _viewArr11.count; i++) {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
[[_viewArr11 objectAtIndex:i] setFrame:CGRectMake(KTableStartPointX + (i%5) * KButtonWidth, KTableStartPointY + (i/5)* KButtonHeight, KButtonWidth, KButtonHeight)];
} completion:^(BOOL finished) {
}];
}
for (int i = 0; i < _viewArr22.count; i++) {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
[[_viewArr22 objectAtIndex:i] setFrame:CGRectMake(KTableStartPointX + (i%5) * KButtonWidth, KTableStartPointY + [self array2StartY] * KButtonHeight + (i/5)* KButtonHeight, KButtonWidth, KButtonHeight)];
} completion:^(BOOL finished){
}];
}
}
- (unsigned long)array2StartY{
unsigned long y = 0;
y = _viewArr11.count/5 + 2;
if (_viewArr11.count%5 == 0) {
y -= 1;
}
return y;
}
这个是你每删除一行就要改变其高度。效果图:
这样你就可以实现频道订阅界面和功能,点击某个频道,可以将其移入或者移出订阅栏,剩下的频道自动重新排列。
这只是最简单的点击某个频道,可以将其移入或者移出订阅栏,还有移动某个频道的时候可以随意插入位置
这个比较复杂,下篇将做详细解答。