아이폰 앱을 보다보면은 아래 그림과 같이 반투명 뷰에 ActivityIndicator를 이용해서 화면에 뿌려주는 것을 볼수 있습니다.
궁금하기도 하고 나중에 써먹을 일이 있을것 같아서 인터넷으로 찾아봤는데 구글신이 외면했는지 못찾아서
짜집기로 직접 만들어 봤습니다.

 

//
//  UIActivityIndicatorRoundView.h
//  AlphaTest
//
//  Created by Dae Jun Ko on 11. 8. 13..
//  Copyright 2011년 __MyCompanyName__. All rights reserved.
//

#import 

#define kDefaultStrokeColor         [[UIColor blackColor] colorWithAlphaComponent:0.5]
#define kDefaultRectColor           [[UIColor blackColor] colorWithAlphaComponent:0.5]
#define kDefaultStrokeWidth         0.0
#define kDefaultCornerRadius        10.0

@interface UIActivityIndicatorRoundView : UIView

@property (nonatomic, retain) UIColor *strokeColor;
@property (nonatomic, retain) UIColor *rectColor;
@property (nonatomic, assign) CGFloat strokeWidth;
@property (nonatomic, assign) CGFloat cornerRadius;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@end


//
//  UIActivityIndicatorRoundView.m
//  AlphaTest
//
//  Created by Dae Jun Ko on 11. 8. 13..
//  Copyright 2011년 __MyCompanyName__. All rights reserved.
//

#import "UIActivityIndicatorRoundView.h"

@implementation UIActivityIndicatorRoundView

@synthesize strokeColor, rectColor;
@synthesize strokeWidth, cornerRadius;
@synthesize activityIndicator;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.opaque = NO;
        self.strokeColor = kDefaultStrokeColor;
        self.rectColor = kDefaultRectColor;
        self.strokeWidth = kDefaultStrokeWidth;
        self.cornerRadius = kDefaultCornerRadius;
        
        self.activityIndicator 
        = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
        CGPoint mycenter;
        mycenter.x = frame.size.width/2;
        mycenter.y = frame.size.height/2;
        self.activityIndicator.center = mycenter;
        [self.activityIndicator startAnimating];
        [self addSubview:activityIndicator];
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, strokeWidth);
    CGContextSetStrokeColorWithColor(context, self.strokeColor.CGColor);
    CGContextSetFillColorWithColor(context, self.rectColor.CGColor);
    
    CGRect rrect = self.bounds;
    
    CGFloat radius = cornerRadius;
    CGFloat width = CGRectGetWidth(rrect);
    CGFloat height = CGRectGetHeight(rrect);
    
    // Make sure corner radius isn't larger than half the shorter side
    if (radius > width/2.0)
        radius = width/2.0;
    if (radius > height/2.0)
        radius = height/2.0;    
    
    CGFloat minx = CGRectGetMinX(rrect);
    CGFloat midx = CGRectGetMidX(rrect);
    CGFloat maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect);
    CGFloat midy = CGRectGetMidY(rrect);
    CGFloat maxy = CGRectGetMaxY(rrect);
    CGContextMoveToPoint(context, minx, midy);
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);
    
    [self bringSubviewToFront:activityIndicator];
}


-(void)dealloc
{
    [activityIndicator release];
    
    [strokeColor release];
    [rectColor release];
    
    
    [super dealloc];
}

@end


// 사용법
#pragma mark-
#pragma mark clickAlert
-(IBAction)startAlert:(id)sender
{
    if ( viewAlert )
        return;
    
    CGRect frame = [self.view frame];
    CGRect newFrame;

    newFrame.size.width = frame.size.width/2;
    newFrame.size.height = frame.size.width/2;
    newFrame.origin.x = (frame.size.width - newFrame.size.width) / 2;
    newFrame.origin.y = (frame.size.height - newFrame.size.height) / 2;
    
    if ( ( viewAlert = [[UIActivityIndicatorRoundView alloc] initWithFrame:newFrame] ) )
    {   
         [self.view addSubview:viewAlert];
    }
}

-(IBAction)stopAlert:(id)sender
{
    if ( viewAlert == nil )
        return;
    
    [viewAlert removeFromSuperview];
    [viewAlert release];
    viewAlert = nil;
}
#pragma mark-


만드는 방법은 여러가지가 있겠으나 전 쿼츠를 이용해서 그렸습니다.
그리는 방법은 아이폰 예제 QuartzDemo와 http://iphonedevelopment.blogspot.com/2008/11/creating-transparent-uiviews-rounded.html를 참조했습니다.

posted by 뚱2