IOS_UI_ImageView Image 手势 UISwitch UISegmentControl

来源:互联网 发布:双色球大数据分析软件 编辑:程序博客网 时间:2024/06/04 17:47

#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 = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    [_window release];

    

    MainViewController *mainVC = [[MainViewControlleralloc]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 {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    //UIImageView 的使用

    //UIImageView 是一个现示图片的类,本身不是图片,而是类相似于相框

    UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(20,20, 335, 600)];

    imageView.backgroundColor = [UIColorgrayColor];

    [self.viewaddSubview:imageView];

    [imageView release];

    //UIImage 是一个图片类,保存了一张图片的所有信息,本身不能显示,需要借助UIImageView显示

    UIImage *image = [UIImageimageNamed:@"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 = [[UISwitchalloc]initWithFrame:CGRectMake(20,20, 50, 20)];

    

    swi.tintColor = [UIColorwhiteColor];

    swi.onTintColor = [UIColorwhiteColor];

    [swi addTarget:selfaction:@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 = [[UISegmentedControlalloc]initWithItems:@[@"red",@"blue",@"yellow"]];

    seg.frame =CGRectMake(20,20, 335, 20);

//    seg.tintColor = [UIColor redColor];//线条颜色

    seg.selectedSegmentIndex =1;

    [seg addTarget:selfaction:@selector(segmentAction:)forControlEvents:UIControlEventValueChanged];

        [self.viewaddSubview:seg];

        [seg release];


    

}

- (void)switchAction:(UISwitch *)swi

{

    NSLog(@"点击");

    if (swi.isOn) {

        [self.imageViewstartAnimating];

    }else{

        [self.imageViewstopAnimating];

    }

    

}

//- (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 = [UIColorredColor];

            break;

        case 1:

            self.view.backgroundColor = [UIColorblueColor];

            break;

        case 2 :

            self.view.backgroundColor = [UIColoryellowColor];

            break;

            

        default:

            break;

    }

}


0 0
原创粉丝点击