2012-03-29 60 views
1

我需要創建多個具有MARQUEE效果的UIView,如HTML <marquee>標記。如何在iphone中創建多個具有選取框效果的uiview動畫

我首先需要動態創建UIView。

爲動態創建的UIView添加MARQUEE效果之後。

任何幫助將不勝感激。

+0

兩件事:你的問題不清楚,你有什麼試過? – 2012-03-29 05:19:19

+0

我正在嘗試動態創建uiview,並且像html html選項標籤一樣水平移動uiview,而不必重複動態uiview ...! – Dinesh 2012-03-29 05:21:35

+0

你的意思是你創建了多個視圖?觀點中會發生什麼? – 2012-03-29 05:22:41

回答

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 
+0

如何使用此函數...!,並顯示此行中的錯誤 - @屬性(強,非原子)NSMutableArray *消息;錯誤消息未知屬性強 – Dinesh 2012-03-29 06:08:21

+0

它針對iOS SDK 5.1(使用ARC)。你可以建立OS5嗎?如果沒有,你可以看看它是如何工作的,通過改變強度來「保留」,但不要這樣運送。它會泄漏內存,除非你做了額外的工作。 – danh 2012-03-29 06:13:44

+0

不,我正在使用ios 4.3 SDK – Dinesh 2012-03-29 06:17:22