2013-02-09 220 views
10

我的應用程序的目標是讓用戶能夠通過滑動iPhone屏幕的左側和右側來排序不同的日程安排選項。當用戶通過這些不同的日程安排選項排序時,如何繪製和刪除矩形?在iOS中繪製矩形

我有一個UIViewController.h,UIViewController.m和UIViewController.xib文件來操縱。我需要一個單獨的UIView類嗎?如果是這樣,我該如何將UIView類連接到我的.xib文件中的視圖?

+0

Xcode是一個應用程序(IDE)。它沒有畫任何東西。 Objective-C是iOS的編程語言。你可能想看看這個網站上的教程 - 特別是這個教程:http://www.raywenderlich.com/2033/core-graphics-101-lines-rectangles-and-gradients(儘管再看看你的問題似乎與繪圖沒什麼關係,也許你想'UIScrollView') – 2013-02-09 05:32:43

+0

據我所知,我需要編寫Objective-C代碼,但我說我是用X代碼作爲參照系。我的理解是UIScrollView只是決定你是否可以滾動,你可以滾動多遠。如果我從屏幕上畫了一個矩形,但是UIScrollView被定義爲可以滾動到最遠,我最終可以查看它。基於這個前提,我只需要知道如何使用UIViewController及其.xib文件根據用戶輸入繪製矩形以確定座標。 – 2013-02-09 05:38:27

+0

我需要的是一種方法,它將接受用戶確定的輸入,並根據用戶定義的座標,高度和寬度繪製矩形。 – 2013-02-09 05:40:39

回答

23

哇...這麼多複雜的解決方案,這裏是你需要的東西:

UIView *myBox = [[UIView alloc] initWithFrame:CGRectMake(180, 35, 10, 10)]; 
myBox.backgroundColor = [UIColor lightGrayColor]; 
[self.view addSubview:myBox]; 

沒有別的......不需要發瘋的實施。

5

首先製作一個CustomView。

#import <UIKit/UIKit.h> 

@interface MyView : UIView 

@end 

#import "MyView.h" 

@implementation MyView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect 
{ 
    // Drawing Rect 
    [[UIColor redColor] setFill];              // red 
    UIRectFill(CGRectInset(self.bounds, 100, 100)); 
} 


@end 

你測試。鏈接到你的AppDelegate或視圖控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{  
    MyView* view = [[MyView alloc]initWithFrame:CGRectMake(10, 30, 300, 400)]; 
    view.backgroundColor = [UIColor blackColor]; 
    [window addSubview:view]; 

    [self.window makeKeyAndVisible]; 

    return YES; 
} 

- (void)viewDidLoad 
{ 
    MyView* view = [[MyView alloc]initWithFrame:CGRectMake(10, 30, 300, 400)]; 
    view.backgroundColor = [UIColor blackColor]; 
    [self.view addSubview:view]; 

    [super viewDidLoad]; 
} 
3

一個UIView發生在一個長方形的形狀繪製,所以如果你需要的是改變矩形的顏色,設置的backgroundColor財產UIView到你想要的顏色。

一個空的UIView會做:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)]; 
view.backgroundColor = [UIColor redColor]; 

如果從界面生成器做這件事,從庫中拖出一個新的視圖到畫布上,並設置在查看其屬性的背景色。

對於簡單情況,您可能不需要自定義視圖。您還可以將其他項目添加到「矩形」視圖中,例如用於顯示文本的UILabels。

+0

這是一個我以前見過的建議,但問題是我需要能夠在應用程序運行時在屏幕上的任意座標處放置一個任意大小的矩形,因爲用戶將輸入座標值和高度和寬度。 – 2013-02-09 05:47:25

+0

您可以在運行時動態更新任何視圖的框架。但是你需要在代碼中這樣做。當你更新框架時,它將改變矩形的位置和大小。它可以是「從字面上任意大小」和「在任何座標上」:) – Kekoa 2013-02-09 05:50:05

+0

是否包含視圖的座標?這也需要能夠具有未定義數量的矩形 – 2013-02-09 05:52:27

11
// 1st Add QuartzCore.framework to your project's "Link Binary with Libraries". 

// 2nd Import the header in your .m file (of targeted view controller): 

#import <QuartzCore/QuartzCore.h> 

// 3rd Inside - (void)viewDidLoad method: 
// Create a UIBezierPath (replace the coordinates with whatever you want): 
UIBezierPath *path = [UIBezierPath bezierPath]; 
[path moveToPoint:CGPointMake(10.0, 10.0)]; 
// # Entry Point with 10px white frame 
[path moveToPoint:CGPointMake(10.0, 10.0)]; 
// # Keeping 10px frame with iPhone's 450 on y-axis 
[path addLineToPoint:CGPointMake(10.0, 450.0)]; 
// # Substracting 10px for frame on x-axis, and moving 450 in y-axis 
[path addLineToPoint:CGPointMake(310.0, 450.0)]; 
// # Moving up to 1st step 10px line, 310px on the x-axis 
[path addLineToPoint:CGPointMake(310.0, 10.0)]; 
// # Back to entry point 
[path addLineToPoint:CGPointMake(10.0, 10.0)]; 

// 4th Create a CAShapeLayer that uses that UIBezierPath: 
CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
shapeLayer.path = [path CGPath]; 
shapeLayer.strokeColor = [[UIColor blueColor] CGColor]; 
shapeLayer.lineWidth = 3.0; 
shapeLayer.fillColor = [[UIColor clearColor] CGColor]; 
Add that CAShapeLayer to your view's layer: 

// 5th add shapeLayer as sublayer inside layer view 
[self.view.layer addSublayer:shapeLayer]; 
+4

你總是可以做[UIBezierPath bezierPathWithRect:(CGRect)rect]; – 2014-07-14 20:50:59

3

您可以使用此畫一個矩形:

UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(68, 38, 81, 40)]; 
[UIColor.grayColor setFill]; 
[rectanglePath fill];