ハートの形や星の形など、特定の形のコラージュをたくさん欲しいのですが、iPhoneアプリでObjective Cを使用してプログラムでこれを行う方法を教えてもらえますか?
質問する
2430 次
2 に答える
1
このコードを使用します。ここで、画像は PhotoLibrary から選択され、ジェスチャを使用して移動、ズームなどを行っています。すべてのジェスチャを使用して、配置した画像の実質的にすべてのパン、ピンチ、回転を行うことができます。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
imageCollage = [[UIImageView alloc]initWithFrame:CGRectMake(80, 80, 150, 150)];
[imageCollage setUserInteractionEnabled:YES];
imageCollage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
holderView = [[UIView alloc] initWithFrame:CGRectMake(0,0,imageCollage.frame .size.width, imageCollage.frame.size.height)];
holderView.layer.cornerRadius = 6;
holderView.clipsToBounds = YES;
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:imageCollage.image];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
DeleteImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
[DeleteImage setImage:[UIImage imageNamed:@"DeleteRed.png"]];
[holderView addSubview:DeleteImage];
DeleteImage.userInteractionEnabled = YES;
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[DeleteImage addGestureRecognizer:tapRecognizer];
[BaseView addSubview:holderView];
[Trash setImage:[UIImage imageNamed:@"12456977871712665073hrum_trash.svg.med.png"] forState:UIControlStateNormal];
}
-(void)scale:(id)sender
{
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];
if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];
lastScale = [(UIPinchGestureRecognizer*)sender scale];
}
-(void)rotate:(id)sender
{
[BaseView bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];
if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
lastRotation = 0.0;
return;
}
CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);
CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];
lastRotation = [(UIRotationGestureRecognizer*)sender rotation];
}
-(void)move:(id)sender
{
[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];
BaseView.clipsToBounds = YES;
[BaseView bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {
CGFloat finalX = translatedPoint.x + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
CGFloat finalY = translatedPoint.y + (.20*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);
if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 260) {
finalX = 260;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 416) {
finalY = 416;
}
}
else {
if(finalX < 0) {
finalX = 0;
}
else if(finalX > 416) {
finalX = 260;
}
if(finalY < 0) {
finalY = 0;
}
else if(finalY > 260) {
finalY = 416;
}
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.35];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[[sender view] setCenter:CGPointMake(finalX, finalY)];
[UIView commitAnimations];
}
}
-(void)tapped:(UIGestureRecognizer *)recogniser
{
NSLog(@"%@",recogniser.view);
[Trash setImage:[UIImage imageNamed:@"trash_bin_recycle_quit_terminate_error_cancel_close_exit.png"] forState:UIControlStateNormal];
[[[recogniser view] superview] removeFromSuperview];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}
タラに関するご質問はお気軽にお問い合わせください :)... Happy Coding ..
于 2012-09-19T10:32:23.383 に答える
0
Core Graphics の経験がほとんどなく、この種の図形をプログラムで生成したい場合は、PaintCodeという Mac アプリを使用することをお勧めします。
これを使用して図形を描画すると、対応する Objective-C 描画コードが生成されます。
于 2012-09-19T10:08:45.887 に答える