IOS UIView子類UIScrollView


  雖然apple在IOS框架中提供了很多可以直接使用的UI控件,但是在實際開發當中我們通常都是要自己去定制UIView的外觀和行為。所以創建UIView的子類是必需的。

  剛開始接觸IOS的開發,先從簡單的做起。自定義的UI類,都要繼承UIView類然后實現或覆蓋其中的方法。我這里把這個類命名為HypnosisterView:

 1 #import <Foundation/Foundation.h>
 2 
 3 @interface HypnosisterView : UIView
 4 
 5 @property (nonatomic,strong) UIColor *circleColor ;
 6 
 7 //overide methods
 8 - (void)drawRect:(CGRect)dirtyRect;
 9 - (id)initWithFrame:(CGRect)frame;
10 @end

  簡單地覆蓋了UIview的兩個方法:drawRect和initWithFrame

- (void)drawRect:(CGRect)dirtyRect
{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect bounds = [self bounds];
    
    CGPoint center ;
    center.x = bounds.origin.x + bounds.size.width/2.0 ;
    center.y = bounds.origin.y + bounds.size.height/2.0 ;
    
    float maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0 ;
    
    CGContextSetLineWidth(ctx,10);
    
    [[self circleColor] setStroke] ;
    
    for(float currentRadius=maxRadius ; currentRadius >0.0 ; currentRadius -= 20.0)
    {
        CGContextAddArc(ctx, center.x, center.y, currentRadius, 0.0, M_PI*2.0, YES);
        CGContextStrokePath(ctx);
    }
    
    //draw some text
    NSString *text = @"You are getting sleepy." ;
    
    UIFont *font = [UIFont boldSystemFontOfSize:28];
    
    CGRect textRect ;
    
    textRect.size = [text sizeWithFont:font] ;
    
    textRect.origin.x = center.x - textRect.size.width / 2.0 ;
    textRect.origin.y = center.y - textRect.size.height / 2.0 ;
    
    [[UIColor blackColor] setFill];
    
    //add show shadow to the text
    CGSize offset = CGSizeMake(4, 3);
    
    CGColorRef color = [[UIColor darkGrayColor] CGColor];
    
    CGContextSetShadowWithColor(ctx, offset, 2.0, color);
    
    //draw rect 
    [text drawInRect:textRect withFont:font];
    
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if(self != nil)
    {
        [self setBackgroundColor:[UIColor clearColor]] ;
        [self setCircleColor:[UIColor lightGrayColor]] ;
    }
    
    return self ;
}

  在屏幕上這個view的樣子。

 幾點要注意:

1,UIView不會自己自動重繪,要對其發送setNeedsDisplay消息進行重繪。

2,關於firstResponder默認是不能稱為FirstResponder的, 要覆蓋其- (BOOL)canBecomeFirstResponder來修改這一默認屬性。

3,對view發送becomeFirstResponser消息讓其成為當前對象的FirstResponder。

4,一個成為FirstResponder以后可以對運動事件響應,不過要實現其中的一些方法,比如:

1 //手機搖動事件
2 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
3 {
4     if(motion == UIEventSubtypeMotionShake)
5     {
6         [self setCircleColor:[UIColor redColor]];
7     }
8 }

 

UIScrollView:

  UIScrollView是一個非常有用的控件,將一個UIView加到上面設置,設置一定的參數就可以實現拖放和分頁。而且UIScollView上面支持多於一個View的顯示。

1,利用scrollview進行分頁:在一個window上面放置幾個不同的頁面(一次只顯示一個),通過的scrollview的滾動來實現分頁的效果。其中有一個屬性可以使scrollview自動對其邊界:[scrollView setPagingEnabled:YES];

舉個例子來說明一下:在一個桌子上面並排放着很多撲克牌,桌子上面有一個窗口和撲克牌同樣大小。那么窗口一次只能夠顯示一張撲克牌,每次通過左右移動就可以看到不同的撲克牌,實現了一種分頁效果。在這里桌子和窗口就是UIScrollView而撲克牌就是頁面,窗口其實也可以認為是用戶看到的屏幕。

 

2,scrollview可以對特定的view進行放大和縮小操作,但是只支持一個在其上面的一個view的縮放操作:實現這個縮放要先設定縮放的最小值和最大值,還要實現UIScollViewDelegate協議中的指定方法。

CGRect windowFrame = [[self window] bounds];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:windowFrame];
    CGRect scrollContentFrame = windowFrame ;
    [scrollView setContentSize:scrollContentFrame.size];
    [scrollView setMinimumZoomScale:1.0];
    [scrollView setMaximumZoomScale:5.0];
    [scrollView setDelegate:self];
    [[self window] addSubview:scrollView];

實現協議:

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return view ;
}

 

 

最后一中隱藏Status bar的方法:

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

 

學習筆記,僅供參考,歡迎交流。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM