IOS 设置消息上下滚动,消息循环滚动视图

#import "NewsView.h"

@interface ViewController (){
    NSArray *rssArray;
}
@property (strong, nonatomic) NewsView *newsView;
@end

@implementation ViewController
@synthesize newsView;

static int countInt=0;
static NSString *notice_index;
- (void)viewDidLoad {
    [super viewDidLoad];
    newsView=[[NewsView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 100.0f)];
    [self.view addSubview:newsView];
    
    rssArray=parkEntity.notice_rows;
                          notice_index=[[rssArray objectAtIndex:0] objectForKey:@"id"];
                          [newsView.titleLabel setText:[[rssArray objectAtIndex:countInt] objectForKey:@"title"]];
                          [newsView.descriptionLabel setText:@"TESTDESCRIPTION"];
                          [newsView.newsButton addTarget:self action:@selector(topNewsInfoClicked:) forControlEvents:UIControlEventTouchUpInside];
                          [UIView animateWithDuration:0.7 delay:0 options:0 animations:^(){
                              newsView.alpha = 0.2;
                              [newsView exchangeSubviewAtIndex:1 withSubviewAtIndex:0];
                              newsView.alpha = 1;
                          } completion:^(BOOL finished){
                          //设置定时器
                              [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(displayNews) userInfo:nil repeats:YES];
                          }];
}

-(void)displayNews{
    countInt++;
//    long num = [rssArray count] >= 3 ? 3:[rssArray count];
    if (countInt >= [rssArray count])
        countInt=0;
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = 0.5f ;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = YES;
    animation.type = @"cube";
    
    notice_index=[[rssArray objectAtIndex:countInt] objectForKey:@"id"];
    
    [newsView.layer addAnimation:animation forKey:@"animationID"];
    [newsView setViewWithTitle:[[rssArray objectAtIndex:countInt] objectForKey:@"title"] description:@"test"];
}
-(void)topNewsInfoClicked:(id)sender{
    WebViewController *vc = [[WebViewController alloc] init];
    vc.url = [NSString stringWithFormat:@"%@%@",PARKNOTICE_URL,notice_index];
    vc.vcTitle = [[rssArray objectAtIndex:countInt] objectForKey:@"title"];
    [self.navigationController pushViewController:vc animated:YES];
}
@end


//NewsView.h
//
//  NewsView.h
//  Created by Jack on 15-10-23.
//  Copyright (c) 2015年 Demo. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface NewsView : UIView
@property (retain, nonatomic) UILabel *titleLabel;
@property (retain, nonatomic) UILabel *descriptionLabel;
@property (retain, nonatomic) UIButton *newsButton;

-(void)setViewWithTitle:(NSString *)title description:(NSString *)description;
@end

//  NewsView.m
//  Created by Jack on 15-10-23.
//  Copyright (c) 2015年 Demo. All rights reserved.
//
#import "NewsView.h"
@implementation NewsView
@synthesize titleLabel;
@synthesize descriptionLabel;
@synthesize newsButton;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //[self setBackgroundColor:[UIColor grayColor]];
        titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 2, 211, 21)];
        titleLabel.textColor=[UIColor grayColor];
        titleLabel.backgroundColor=[UIColor clearColor];
        titleLabel.font=[UIFont boldSystemFontOfSize:13.0];
//        titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
        titleLabel.numberOfLines = 1;
        [self addSubview:titleLabel];
        descriptionLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 17, 260, 15)];
        descriptionLabel.textColor=[UIColor blackColor];
        descriptionLabel.backgroundColor=[UIColor clearColor];
        descriptionLabel.font=[UIFont systemFontOfSize:11.0];
        descriptionLabel.lineBreakMode = UILineBreakModeWordWrap;
        descriptionLabel.numberOfLines = 1;
        [self addSubview:descriptionLabel];
        newsButton=[UIButton buttonWithType:UIButtonTypeCustom];
        [newsButton setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:newsButton];
    }
    return self;
}
-(void)dealloc{
//    [titleLabel release];
//    [descriptionLabel release];
//    [super dealloc];
}
-(void)setViewWithTitle:(NSString *)title description:(NSString *)description{
    [titleLabel setText:title];
    [descriptionLabel setText:description];
}
@end

ps:参考

http://blog.csdn.net/totogo2010/article/details/8501812












转载于:https://my.oschina.net/jack088/blog/521951

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS中,我们可以使用滚动视图UIScrollView)来创建一个可以进行滚动的内容视图。在该滚动视图中添加按钮,可以通过以下步骤实现: 1. 创建滚动视图对象:使用`UIScrollView`类创建一个滚动视图对象,可以通过代码或者图形化界面编辑器进行创建。 2. 设置滚动范围:通过设置滚动视图的`contentSize`属性,确定滚动内容的大小。内容大小应该大于滚动视图的可见区域,这样才能进行滚动。例如,`scrollView.contentSize = CGSizeMake(320, 600)`。 3. 添加按钮:创建按钮对象,并设置按钮的位置和样式。例如,`UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 50)]`。 4. 将按钮添加到滚动视图中:使用`addSubview`方法将按钮对象添加到滚动视图中。例如,`[scrollView addSubview:button]`。 5. 设置按钮的位置:通过设置按钮的`frame`属性,确定按钮在滚动视图中的位置。需要注意的是,按钮的位置是相对于滚动视图的坐标系的。例如,`button.frame = CGRectMake(50, 50, 100, 50)`。 6. 设置滚动视图的代理:如果需要监听按钮的点击事件,可以将滚动视图的`delegate`属性设置为当前的视图控制器,并实现`UIScrollViewDelegate`协议。例如,`scrollView.delegate = self`。 7. 监听按钮的点击事件:在滚动视图的代理方法中,根据需要实现按钮的点击事件处理逻辑。例如,对于点击事件的处理可以通过`UIButton`的`addTarget:action:forControlEvents:`方法或者手势识别器来完成。 以上就是在iOS中使用滚动视图添加按钮的基本步骤。根据需要,可以根据具体情况进行定制和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值