プログラマメモ2 - programmer no memo2

中心点からの角度 2011/10/22

以前書いた記事から プログラマメモ2: 中心点からの角度
今度は、objective-cで作成してみました。

#import "MyView.h" @implementation MyView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } /* タッチイベント:touchesBegan */ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self setNeedsDisplay]; } /* タッチイベント:touchesBegan */ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self setNeedsDisplay]; } /* タッチイベント:touchesMoved */ - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { // CGPoint touchPoint = [[touches anyObject] locationInView:self]; [self setNeedsDisplay]; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); float cx = self.bounds.size.width/2; float cy = self.bounds.size.height/2; // 文字色 CGContextSetRGBFillColor(context, 0, 0, 0, .2); // 線色 CGContextSetRGBStrokeColor(context, 0, 0, 0, .2); double degree; { // 初期値(中心値からの角度をもとめる) int mx = touchPoint.x - cx; int my = touchPoint.y - cy; if (mx == 0 && my == 0) { degree = 0; } else { degree = atan2(my, mx) * (180 / M_PI); } } { // line CGContextMoveToPoint(context, cx, cy); CGContextAddLineToPoint(context, touchPoint.x,touchPoint.y); CGContextStrokePath(context); } { // string NSString *str1 = [NSString stringWithFormat:@"x=%.0f, y=%.0f", touchPoint.x, touchPoint.y]; NSString *str2 = [NSString stringWithFormat:@"degree:%.0f", degree]; NSString *str3 = [NSString stringWithFormat:@"角度(0~360):%.0f", degree < 0 ? (180 + degree) + 180:degree]; UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:20]; [str1 drawAtPoint:CGPointMake(touchPoint.x, touchPoint.y) withFont:font]; [str2 drawAtPoint:CGPointMake(touchPoint.x, touchPoint.y+20) withFont:font]; [str3 drawAtPoint:CGPointMake(touchPoint.x, touchPoint.y+40) withFont:font]; } } @end

刻印文字っぽいのその1 2011/10/18

刻印文字っぽいのにチャレンジ


CGContextSetShadowWithColorをいろいろ駆使するとコーディングでいろいろ表現できる。
CSS3?のtext-shadow?で表現していることを参考にしています。
- (void)drawRect:(CGRect)rect { NSLog(@"*** override drawRect!!"); // コンテキストをゲット CGContextRef context = UIGraphicsGetCurrentContext(); { /* * グラデーション */ // コンテクスト保存 CGContextSaveGState(context); if(TRUE){// CGGradientを生成する CGGradientRef gradient; CGColorSpaceRef colorSpace; size_t num_locations = 2; CGFloat locations[2] = { 0.0, 1.0 }; // FFA500 FF4500 CGFloat components[8] = { 0xFF/255., 0xA5/255., 0x00/255., 1.0, // Start color 0xFF/255., 0x45/255., 0x00/255., 1.0 }; // End color colorSpace = CGColorSpaceCreateDeviceRGB(); gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, num_locations); CGPoint startPoint = CGPointMake(self.frame.size.width/2, 0.0); CGPoint endPoint = CGPointMake(self.frame.size.width/2, self.frame.size.height); CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); // GradientとColorSpaceを開放する CGColorSpaceRelease(colorSpace); CGGradientRelease(gradient); } // コンテクスト復元 CGContextRestoreGState(context); } { /* * 文字列を描画して影付けしている * */ // コンテクスト保存 CGContextSaveGState(context); UIColor *color1 = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.2]; UIColor *color2 = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8]; CGContextSetShadowWithColor(context, CGSizeMake(-1.0f, 1.0f), 0.0f, [color1 CGColor]); CGContextSetShadowWithColor(context, CGSizeMake(1.0f, 1.0f), 0.0f, [color2 CGColor]); CGContextSetRGBFillColor (context, 0, 0, 0, 1); // UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:40]; NSString *str = @"HelooWorkd"; [str drawAtPoint:CGPointMake(10, 10) withFont:font]; // コンテクスト復元 CGContextRestoreGState(context); } }

デバイスの向きがかわって回転させないようにしたい 2011/10/15
2011/10/23

iosです。
デバイスの向きがかわって回転させないようにしたいです。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return false; }

すぐ忘れてしまうんですよね...
2011/10/23 追記

上記のコードだと、「It should support at least one orientation.」と警告ログがでる。
iphone - Ipad device orientation problem - Stack Overflow
方向を固定させたい向きかどうかで判定させるのがいいのかも。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); }

角丸こそわが人生 2011/10/08

shiffonの備忘録 - UILabelで角丸
2011-04-13 - spiratestaの日記
UILabelを角丸にしたいなーといことで。
やはり角丸にしたいよねということです。


QuartzCoreを使うようだ。
#import "QuartzCore/QuartzCore.h"

コードは
- (void)viewDidLoad { [super viewDidLoad]; [[label layer] setCornerRadius:6.0]; [label setClipsToBounds:YES]; }

参考:
いつかつかってみたい
Cocoaの日々: バッジ描画ライブラリを公開