在`objective c`中有`marquee`标签的替代方法吗?

时间:2015-07-16 07:40:06

标签: ios objective-c

我想将label的文字从左向右移动,反之亦然。意味着我在这里找到marquee标签的替代品。有没有解决方案。

enter image description here

代码:

-(UIView*)otherView:(UIView *)oview
{  // backgroundImgLogin
    UIImageView *backgroundImgLogin=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, oview.frame.size.width, oview.frame.size.height)];
    backgroundImgLogin.image=[UIImage imageNamed:@"green-Home1.jpg"];

    [oview addSubview:backgroundImgLogin];

    //welcome Label
    UILabel *welcomeLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,45+45+18, 1, 35)];
    [welcomeLabel setText:@"Please enter your login ID and password to continue"];
    [welcomeLabel setTextColor:[UIColor whiteColor]];
    [welcomeLabel setFont:[UIFont fontWithName:@"Helvetica" size:20]];
    [welcomeLabel sizeToFit];

    [oview addSubview:welcomeLabel];
    return oview;
}

1 个答案:

答案 0 :(得分:1)

我认为解决方案并不太复杂,因为您必须考虑滚动是x轴上任意值的转换。
这意味着一步一步,也许使用UIView动画,你必须添加(滚动到图形上下文的右侧)任意值到标签的当前x值,如果你想在左边移动,减去它。
如果您愿意,可以在此处下载Objective-c marquee实现的完整源代码
https://github.com/alchimya/iOS-Marquee
我在这里粘贴一个obj-c方法(我希望!)可以帮助你。

- (void)scrollContentView:(UIView *)content toDirection:(L3SDKMarqueeDirection)方向withSpeed:(float)speed {

CGRect newFrame;
CGRect resetFrame;

//defines the amount of the translation (scrolling) to the x axis
int xProgression=15;


float newX=0;
float resetX=0;

switch (direction) {
    case L3SDKMarqueeDirectionLeft:
        //if the scrolling happens to the left, to define a new translate frame
        //we have to subtract the translation amount (xProgression) from the current x position
        newX=content.frame.origin.x-xProgression;
        //when the scroll will be completed (out of the left side) we have to restart
        //the scrolling from the right side
        resetX=self.frame.size.width;
        break;
    case L3SDKMarqueeDirectionRight:
        //if the scrolling happens to the right, to define a new translate frame
        //we have to add the translation amount (xProgression) from the current x position
        newX=content.frame.origin.x+xProgression;
        //when the scroll will be completed (out of the right side) we have to restart
        //the scrolling from the left side
        resetX=self.frame.origin.x-content.frame.size.width;
        break;
    default:
        break;
}

//defines the scrolling frame
newFrame=CGRectMake(
                    newX,
                    content.frame.origin.y,
                    content.frame.size.width,
                    content.frame.size.height
                    );
//this frame will be applied to the content view to restart the scolling
//on the opposite side
resetFrame=CGRectMake(
                      resetX,
                      content.frame.origin.y,
                      content.frame.size.width,
                      content.frame.size.height
                      );

//defines an animation
[UIView animateWithDuration:speed
                      delay:0.0
                    options:UIViewAnimationOptionCurveLinear
                 animations:^{
                     content.frame =newFrame;
                 }
                 completion:^(BOOL finished){
                     BOOL isResetFrame=NO;

                      //checks if the scrolling is completed on the right/left side
                     // and restart the scrolling on the opposite side
                     switch (direction) {
                         case L3SDKMarqueeDirectionRight:

                             if (content.frame.origin.x >=self.frame.size.width) {
                                 isResetFrame=YES;
                             }
                             break;
                         case L3SDKMarqueeDirectionLeft:
                             if ((content.frame.origin.x+content.frame.size.width)<=-self.frame.origin.x) {
                                 isResetFrame=YES;
                             }
                             break;
                         default:
                             break;
                     }

                     if (isResetFrame) {
                         content.frame = resetFrame;
                     }
                     //recursive call
                     [self scrollContentView:content toDirection:direction withSpeed:speed];
                 }];

}