プログラマメモ2

2012-01-31

なんでもかんでもxxx.pchに含めてしまえ - xcode


最近、まったく自宅でコーディングしたりコンピュータで遊べてません。
やばいなーと思います。
さて、Xcodeをまったく使いこなせてません。その上Objective-Cは悲しいほど上達していない。。。
で、import文を追加するのが面倒だと思うときにはもうxxx.pchにほうりこんでしまえばよろしいと暴言をはいてしまいたい。
少しでも自分のコーディングが気分よくできればそれでよし。
pchはPre Compile Headerの略?




2012-01-16

substring - java


Javaです。 
Stringのsubstringで java.lang.StringIndexOutOfBoundsExceptionが発生しやすいことをついつい忘れてしまってたりします。
桁数が足りなかったりするとすぐに例外していまいます。
こんなときJavaって固いなーと思ったりします(いい意味でですよ)。
で、そんなときのcommonsのStringUtilsのsubstring
http://commons.apache.org/lang/

import org.apache.commons.lang3.StringUtils; public class TestCommonsLangStringUtils { public static void main(String[] args) { a(); } static void a() { String s = "01234567"; // このコードは java.lang.StringIndexOutOfBoundsException: String index out of range: 10 // String s2 = s.substring(3, 10); String s3 = StringUtils.substring(s, 3, 10); System.out.println(s3); } }




2012-01-03

2012年 あけましておめでとうございます。


あけましておめでとうございます。2012年はどーんと飛躍の年になったらいいなと思います。
2011年はいろいろ大変な年でしたね。

2011年12月に記事をアップすることができず、はじめてから、記事がない月がはじめて(たぶん)でました。
いろいろ作りたいものもあるのですが、なんせ未熟なものでなかなか完成できず
今年は、とにもかくにも完成できるものをひとつづつ増やしていこうと思います。




2011-11-20

CALayerでsetNeedsDisplayするときの注意


CALayerにたいしてsetNeedsDisplayするときの注意です。
下記のようなコードをつかって、表示する場合にsetNeedsDisplayしたりすると、表示されなくなる。

UIImage *img1 = [UIImage imageNamed:@"aaa.png"]; CALayer *layer = [CALayer layer]; layer.contentsScale = [[UIScreen mainScreen] scale]; layer.contents = (id)img1.CGImage;
おそらく,setNeedsDisplayが
レイヤインスタンスにコ ンテンツをキャッシュし直す
させるということに関係するのかな。




2011-11-13

CALayerのretina対応 contentsScaleを2


core animationだ!!ということで、CALayerのサブクラスをつくり、drawInContextを実装してがりがり書いて動かすとどうも、ジャギーが目立つというかなんというか表示したイメージきれいでないことに気がつく。 で、アンチエイリアスが効いてないのかなーとかとんちんかんな調べものをしていてようやく、contentsScaleを変更すればいいところにたどりつく。

おそらく正しいやり方は
[[UIScreen mainScreen] scale];
をlayaer.contentsScaleに設定するのが正しいやり方のだろう。
参考
OpenGL ESのRetina対応 - Objective-Audio
iphone - Retina display core graphics font quality - Stack Overflow




2011-11-03

UIBarButtonItemに画像使いたいなー


InterfaceBuilderをぜんぜんつかってなかったのですが、やはりウィジットを配置してすぐにためせるのはやはりInterfaceBuilder便利。 UIBarButtonItemに画像使いたいなーと思っていろいろいじってたら、IBで簡単に設定できました。 ImageInset使ってサイズを調整できます。




2011-10-23

中心点からの角度


以前書いた記事から プログラマメモ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




2011-10-19

刻印文字っぽいのその1


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


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-16

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


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-09

角丸こそわが人生


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の日々: バッジ描画ライブラリを公開




2011-09-24

1012 - ヨゼフ?時間超過


1012 -- Joseph
Javaです。 なんとかそれっぽい答えがでるようになって、submitすると時間超過....
グーグルさんに尋ねて調べると、あらかじめ計算させてその値をつかっていたりしてるようでした。
とりあえずkが10よりうえのものは計算済みのものを使っています。

package p1012; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { static class P { boolean dead = false; boolean goodP = false; public P(boolean goodP) { this.goodP = goodP; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true) { int k = scanner.nextInt(); if (k == 0) break; List<P> list = new ArrayList<Main.P>(); for (int i = 0; i < k; i++) { list.add(new P(true)); } for (int i = k; i < k * 2; i++) { list.add(new P(false)); } int[] is = { 0, 0, 0, 0, 0, 0, 0, 0, 1740, 93313, 459901, 1358657, 2504881 }; if (10 <= k) { System.out.println(is[k - 1]); continue; } int m = 0; A: for (m = 2; m < 100000000; m += 1) { int pos = 0; List<P> list2 = new ArrayList<Main.P>(list); int deadcount = k * 2; while (deadcount != k) { pos += m - 1; if (list2.size() <= pos) { pos = pos % list2.size(); } if (pos < k) continue A; list2.remove(pos); deadcount--; } break A; } System.out.println(m); } } }




 

プログラマの本棚