#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
MainViewController *mainVC = [[MainViewController alloc]init];
self.window.rootViewController = mainVC;
[mainVC release];
return YES;
}
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@end
#import "MainViewController.h"
@interface MainViewController ()
@property (nonatomic,retain)UIImageView *imageView;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//UIImageView 的使用
//UIImageView 是一个现示图片的类,本身不是图片,而是类相似于相框
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 335, 600)];
imageView.backgroundColor = [UIColor grayColor];
[self.view addSubview:imageView];
[imageView release];
//UIImage 是一个图片类,保存了一张图片的所有信息,本身不能显示,需要借助UIImageView显示
UIImage *image = [UIImage imageNamed:@"IMG_8105.JPG"];
//使用UIImageView 显示一张图片
imageView.image = image;
//imageView 和Label 默认不开用户交互 所以要手动开
imageView.userInteractionEnabled = YES;
//手势
//点击
// UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//
// tap.numberOfTapsRequired = 2;//设置双击
// tap.numberOfTouchesRequired = 2;//两个手指点两下,在模拟器上 两个手指用alt+鼠标
// [imageView addGestureRecognizer:tap];
// [tap release];
// //长按
// UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
// //设置多久算长按 单位秒
// longPress.minimumPressDuration = 0.5;
// //长按时允许手指移动距离 默认为10像素
// longPress.allowableMovement = 200;
//
// [imageView addGestureRecognizer:longPress];
// [longPress release];
// //轻扫
// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
// //识别滑动方向 每个程序只能设计一个方向 如果四个都需要则需要设计四个程序
// swipe.direction = UISwipeGestureRecognizerDirectionUp;
swipe.direction = UISwipeGestureRecognizerDirectionDown;
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
swipe.direction = UISwipeGestureRecognizerDirectionRight;
//
// [imageView addGestureRecognizer:swipe];
// [swipe release];
//旋转
// UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
// [imageView addGestureRecognizer:rotation];
// [rotation release];
//捏合
// UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
// [imageView addGestureRecognizer:pinch];
// [pinch release];
//拖拽
// UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
// [imageView addGestureRecognizer:pan];
// [pan release];
//升级版
// UIScreenEdgePanGestureRecognizer *pan1 = [[ UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction1:)];
// [imageView addGestureRecognizer:pan1];
// [pan1 release];
//UISwitch
UISwitch *swi = [[UISwitch alloc]initWithFrame:CGRectMake(20, 20, 50, 20)];
swi.tintColor = [UIColor whiteColor];
swi.onTintColor = [UIColor whiteColor];
[swi addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
[imageView addSubview:swi];
[swi release];
swi.on = YES;
//segment
// NSMutableArray *array = [NSMutableArray alloc] initWithObjects:@"red",@"blue",@"yellow"];
// UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:array];
// seg.frame = CGRectMake(20, 20, 335, 20);
// seg.backgroundColor = [UIColor yellowColor];
// [seg addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
// [self.view addSubview:seg];
// [seg release];
UISegmentedControl *seg = [[UISegmentedControl alloc]initWithItems:@[@"red",@"blue",@"yellow"]];
seg.frame =CGRectMake(20, 20, 335, 20);
// seg.tintColor = [UIColor redColor];//线条颜色
seg.selectedSegmentIndex = 1;
[seg addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:seg];
[seg release];
}
- (void)switchAction:(UISwitch *)swi
{
NSLog(@"点击");
if (swi.isOn) {
[self.imageView startAnimating];
}else{
[self.imageView stopAnimating];
}
}
//- (void)tapAction:(UITapGestureRecognizer *)tap
//{
// NSLog(@"点击手势");
//}
//- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
//{
//
// if ( longPress.state == UIGestureRecognizerStateBegan)
// {NSLog(@"长按");}
//}
//- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
//{
// NSLog(@"轻扫");
//}
//- (void)rotationAction:(UIRotationGestureRecognizer *)rotationAction
//{
NSLog(@"%f",rotationAction.rotation);
// UIView *view = rotationAction.view;
// view.transform = CGAffineTransformRotate(view.transform ,rotationAction.rotation);
// rotationAction.rotation = 0;
//
// NSLog(@"旋转");
//}
//- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
//{
// UIView *view = pinch.view;
// view.transform = CGAffineTransformScale(view.transform, pinch.scale, pinch.scale);
// pinch.scale = 1;
// NSLog(@"捏合");
//}
//- (void)panAction:(UIPanGestureRecognizer *)pan
//{
// UIView *view = pan.view;
// CGPoint p = [pan translationInView:view];
// view.transform = CGAffineTransformTranslate(view.transform, p.x, p.y);
// [pan setTranslation:CGPointZero inView:view];
// NSLog(@"拖拽");
//}
//- (void)panAction1:(UIPanGestureRecognizer *)pan1
//{
// UIView *view = pan1.view;
// CGPoint p = [pan1 translationInView:view];
// view.transform = CGAffineTransformTranslate(view.transform, p.x, p.y);
// [pan1 setTranslation:CGPointZero inView:view];
// NSLog(@"拖拽");
//}
- (void)segmentAction:(UISegmentedControl *)segment
{
NSLog(@"点击");
switch (segment.selectedSegmentIndex) {
case 0:
self.view.backgroundColor = [UIColor redColor];
break;
case 1:
self.view.backgroundColor = [UIColor blueColor];
break;
case 2 :
self.view.backgroundColor = [UIColor yellowColor];
break;
default:
break;
}
}