新闻的频道订阅界面和功能

实现了凤凰新闻的频道订阅界面和功能,点击某个频道,可以将其移入或者移出订阅栏,剩下的频道自动重新排列。


原理:
1、把所有的频道放在一个数组中,创建ui界面  显示出如下所示:



创建的核心代码

    _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;

}

这个是你每删除一行就要改变其高度。效果图:



这样你就可以实现频道订阅界面和功能,点击某个频道,可以将其移入或者移出订阅栏,剩下的频道自动重新排列。


这只是最简单的点击某个频道,可以将其移入或者移出订阅栏,还有移动某个频道的时候可以随意插入位置


这个比较复杂,下篇将做详细解答。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值