2014-02-20 23 views
2

在iOS应用程序中从下往上移动字幕样式的字幕样式。我已经尝试了很长一段时间使用谷歌搜索,但我不能得到完美的答案这个问题,请提供任何代码为这个问题。我是一个新的iOS应用程序。从下往上移动文字,如使用textView的iOS应用程序中的字幕样式

+0

检查我对这个问题的答案你刚刚有只TIMEDURATION完成动画1时则设置重复 –

+0

后哇.. !!你很聪明。 – Optimistic

回答

1

试试这个

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

NSTimer *timer; 
UILabel *label ; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    timer =[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(scrollText:) userInfo:nil repeats:YES]; 
    label = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 250, 20)]; 

    label.text [email protected]"This is a string to be scroll"; 
    [self.view addSubview:label]; 

} 

-(void)scrollText:(id)parameter{ 
    if (label.frame.origin.y <= 50) { 
     [label setFrame:CGRectMake(label.frame.origin.x, 400, label.frame.size.width, label.frame.size.height)]; 
    } 
    else 
    [label setFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y-5, label.frame.size.width, label.frame.size.height)]; 
} 

@end 
+0

感谢您的回答了我的工作..... – user3331020

+0

我有一个疑问......文字不能完全滚动到顶部.... – user3331020

+0

让您的标签y位置为0 [的UILabel页头] initWithFrame:方法CGRectMake(20 ,0,250,20)];并在scrollText:方法if(label.frame.origin.y <= 0){[label setFrame:CGRectMake(label.frame.origin.x,self.view.frame.size.height - 20,label.frame.size .WIDTH,label.frame.size.height)];} – Rajesh

0

您可以在下面的链接中找到相同的实现。这对于COCOA CONTROL来说非常棒。

COCOA CONTROLS

1

检查这个答案。

.H

NSTimer *timer; 

float timeDuration; 

.M

-(void)viewDidLoad { 

[super viewDidLoad]; 
timeDuration=1.0f; 
timer = [NSTimer scheduledTimerWithTimeInterval:timeDuration target:self selector:@selector(marqueeAnimation) userInfo:nil repeats:YES]; 
} 

-(void)marqueeAnimation{ 

UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, -100, 100, 100)]; 
[imgView setImage:[UIImage imageNamed:@"root.PNG"]]; 
[self.view addSubview:imgView]; 

NSString *keyPath = @"transform.translation.y"; 

CAKeyframeAnimation *translation = [CAKeyframeAnimation animationWithKeyPath:keyPath]; 

translation.duration = timeDuration; 
translation.autoreverses = NO; 
NSMutableArray *values = [[NSMutableArray alloc] init]; 
[values addObject:[NSNumber numberWithFloat:0.0f]]; 
CGFloat height = [[UIScreen mainScreen] applicationFrame].size.height; 
[values addObject:[NSNumber numberWithFloat:height]]; 
translation.values = values; 

NSMutableArray *timingFunctions = [[NSMutableArray alloc] init]; 
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]]; 
translation.timingFunctions = timingFunctions; 

[imgView.layer addAnimation:translation forKey:keyPath]; 

} 
+0

感谢您的重播.... – user3331020

+1

哇.. !!你很聪明。 – Optimistic

0

试试这个。这工作。在示例应用程序中尝试此操作,然后将其包含在您的应用程序中

#import "ViewController.h" 

@interface ViewController() 
@property(nonatomic, retain) NSTimer *timer; 
@end 

@implementation ViewController 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [_txtView setText:@"hi..."]; 
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(marqueeScroll) userInfo:nil repeats:YES]; 
} 

-(void) marqueeScroll 
{ 
    [_txtView setFrame:CGRectMake(_txtView.frame.origin.x, _txtView.frame.origin.y-1.0, _txtView.frame.size.width, _txtView.frame.size.height)]; 
    CGRect screen = [[UIScreen mainScreen] bounds]; 

    if(_txtView.frame.origin.y <= 0) 
    { 
     [_txtView setFrame:CGRectMake(_txtView.frame.origin.x, screen.size.height,_txtView.frame.size.width, _txtView.frame.size.height)] ; 
    } 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

让我知道是否有任何错误。

+0

它不适合我...... – user3331020

+0

你在示例项目中尝试过吗?它给了什么错误?让我知道 – z22

+0

文字不会显示..... – user3331020

相关问题