【问题标题】:How To Create Dynamic more than one uiview with Marquee Effect in iphone如何在 iphone 中使用选取框效果创建多个动态 uiview
【发布时间】:2012-04-12 18:00:28
【问题描述】:

我需要创建多个具有 MARQUEE 效果的 UIView,例如 HTML <marquee> 标签。

我首先需要动态创建 UIView。

为动态创建的 UIView 添加 MARQUEE 效果后。

任何帮助将不胜感激。

【问题讨论】:

  • 两件事:你的问题不清楚,你尝试了什么?
  • 我正在尝试动态创建 uiview 并像 html marquee 标签一样水平移动 uiview 而不重复动态 uiview...!
  • 您的意思是您要创建多个视图?视图中会有什么内容?
  • 是的,我正在尝试像游戏一样点击动态uiview来隐藏点并通过水平移动重新生成动态uiview..!

标签: iphone ios uiview effect marquee


【解决方案1】:

您的意思是一个视图可以显示比视图宽度更宽的字符串集合,通过从右到左缓慢滚动它们来显示?你需要建造它。我做过一次,像这样子类化 UIScrollView:

// CrawlView.h

#import <UIKit/UIKit.h>

@interface CrawlView : UIScrollView

@property (assign, nonatomic) NSTimeInterval period;
@property (strong, nonatomic) NSMutableArray *messages;

- (void)go;

@end


// CrawlView.m

#import "CrawlView.h"

// distance between neighboring strings.  could make this a public property
#define kPADDING 16.0

@interface CrawlView ()

@property (assign, nonatomic) CGFloat messagesWidth;

@end

@implementation CrawlView

@synthesize period=_period;
@synthesize messages=_messages;
@synthesize messagesWidth=_messagesWidth;

- (void)buildSubviews {

    for (UIView *subview in [self subviews]) {
        if ([subview isKindOfClass:[UILabel self]]) {
            [subview removeFromSuperview];
        }
    }

    CGFloat xPos = kPADDING;

    for (NSString *message in self.messages) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.text = message;
        CGSize size = [message sizeWithFont:label.font];
        CGFloat width = size.width + kPADDING;
        label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
        [self addSubview:label];
        xPos += width;
    }
    self.messagesWidth = xPos;
    self.contentSize = CGSizeMake(xPos, self.frame.size.height);
    self.contentOffset = CGPointMake(-self.frame.size.width, 0.0);
}

- (void)setMessages:(NSMutableArray *)messages {

    if (_messages != messages) {
        _messages = messages;
        [self buildSubviews];
    }
}

- (void)go {

    if (!self.period) self.period = self.messagesWidth / 100;
    // so it always takes about the same (fudged, but reasonable) amount of time to scroll the whole array

    [UIView animateWithDuration:self.period
                          delay:0.0
                        options:UIViewAnimationOptionCurveLinear |UIViewAnimationOptionRepeat
                     animations:^{
                         self.contentOffset = CGPointMake(self.messagesWidth, 0.0);
                     } completion:^(BOOL finished){}];
}


@end

【讨论】:

  • 如何使用这个函数...!,它在这一行显示错误-@property (strong, nonatomic) NSMutableArray *messages;错误消息是未知属性强
  • 它针对 iOS SDK 5.1(使用 ARC)。你可以为 OS5 构建吗?如果没有,您可以通过将 strong 更改为“retain”来查看它是如何工作的,但不要以这种方式发送。除非你做额外的工作,否则它会泄漏内存。
  • 哦,所以你必须对这段代码进行一些修改,以确保它正确释放内存。但它可能会在演示模式下通过更改为“保留”而不是强来为您工作。像这样使用它:
  • 通过 IB 或添加滚动视图的方式将其放置在视图上... 1) CrawlView *cv = [[CrawlView alloc] initWithFrame:CGRectMake(0,40,320,30)]; 2) [self.view addSubView:cv]; 3) cv.messages = [NSArray arrayWithObjects:@"hi", "there", @"dinesh", nil]; 4)[简历去]; 5)【简历发布】; //弧前
猜你喜欢
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 2021-07-07
相关资源
最近更新 更多