2013-02-09 15 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-andgradients(あなたの質問をもう一度見ても –

+0

私はobjective-cコードを書く必要があると私は理解していますが、私は参照のフレームとしてx-codeになっていると言っていました。 UIScrollViewは、スクロールできるかどうか、スクロールできるかどうかを判断します。私は、画面の矩形を描いたが、私はそれを最終的に見ることができるまでスクロールできることとして定義されていたUIScrollViewを持っていた場合。この前提に基づいて、UIViewControllerとその.xibファイルを使用して座標を決定するユーザー入力に応じて四角形を描画する方法を知っておくだけです。 –

+0

私が必要とするのは、ユーザーが決めた入力を受け取り、ユーザーが定義した座標、高さ、および幅に基づいて矩形を描く方法です。 –

答えて

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 

ますテスト。あなたが必要とするすべてはのbackgroundColorプロパティを設定し、四角形の色を変更することがあるので、もしあなたのAppDelegateかのViewController

- (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へのリンクは、長方形の形状で描くように起こりますあなたがしたい色にUIView。

空のUIViewが行います。

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

あなたがインターフェイスビルダーからそれをやっている場合は、キャンバスにライブラリから新しいビューをドラッグして、ビューのプロパティで背景色を設定します。

単純なケースのカスタムビューは必要ありません。テキストを表示するために、UILabelsなどの「長方形」ビューに項目を追加することもできます。

+0

これは私が以前見たことのある提案ですが、問題は、ユーザーが座標値と座標値を入力するため、アプリケーションが実行されているときに画面上の任意の座標に文字通り任意のサイズの四角形を配置できる必要があります。高さと幅。 –

+0

実行時に任意のビューのフレームを動的に更新することができます。しかし、コードでそれを行う必要があります。フレームを更新すると、四角形の位置とサイズが変更されます。それは "文字どおりの任意のサイズ"と "任意の座標"にすることができます:) – Kekoa

+0

にはビューの座標が含まれていますか?これはまた、未定義の矩形数を持つことができる必要があります。 –

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]; –

3

あなたはこれを使用して四角形を描くことができます:

UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(68, 38, 81, 40)]; 
[UIColor.grayColor setFill]; 
[rectanglePath fill]; 
関連する問題