黑马程序员--UIiImageView实现动画播放

本文通过实例演示了如何使用UIKit中的UIImageView实现动画效果,并探讨了UIImageView的加载方式及内存优化技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
下面依旧是边敲边整理的内容,我感觉这样看更生动形象,你觉得呢?
这里面也用的addTarget,这个东西就是你想使用它之后,还想有别的效果,那么它的价值就来了!

需要注意的是:用imageNamed,系统会把图片cache到内存中,这就意味着会比较耗费内存,所以这种方式适用于那种反复利用且图片较小的图片,如果,利用次数少,而且还特别大的图片最好用imageWithData这种数据的方式加载


@implementation LJLAppDelegate
{
    UIImageView *_uiimageview1;

}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIImage *image=[UIImage imageNamed:@"r.png"];
    UIImageView *imageview=[[UIImageView alloc]initWithImage:image];
    imageview.frame=CGRectMake(10, 30, 300, 100);//image.size.width设置原图模式
    imageview.backgroundColor=[UIColor greenColor];
    imageview.contentMode=UIViewContentModeScaleAspectFit;//原图显示模式UIViewContentModeScaleAspectFit-把图片变成适应uiview高度
    
    
    NSMutableArray *images=[[NSMutableArray alloc]init];
    int i;
    for (i=1; i<=6; i++) {
        UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];
        [images addObject:image];
    }//加载图片
    
    _uiimageview1=[[UIImageView alloc]init];
    _uiimageview1.frame=CGRectMake(10, 150, 300, 200);
    _uiimageview1.animationImages=images;
    _uiimageview1.contentMode=UIViewContentModeScaleAspectFit;
    _uiimageview1.animationDuration=3;
    _uiimageview1.animationRepeatCount=0;
    _uiimageview1.backgroundColor=[UIColor grayColor];
    
    //start
    [_uiimageview1 startAnimating];
    
    UIButton *stopbtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    stopbtn.frame=CGRectMake(100, 400, 80, 30);
    [stopbtn setTitle:@"停止动画" forState:UIControlStateNormal];
    [stopbtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    stopbtn.tag=1;
    
    UIButton *stopbtn1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    stopbtn1.frame=CGRectMake(200, 400, 100, 30);
    stopbtn.backgroundColor=[UIColor redColor];
    [stopbtn1 setTitle:@"报告状态" forState:UIControlStateNormal];
    [stopbtn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    stopbtn1.tag=2;
    
    UIButton *stopbtn2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    stopbtn2.frame=CGRectMake(10, 400, 80, 30);
    [stopbtn2 setTitle:@"开始动画" forState:UIControlStateNormal];
    [stopbtn2 setBackgroundColor:[UIColor greenColor]];
    [stopbtn2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [stopbtn2 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [stopbtn2 setTintColor:[UIColor whiteColor]];
    [stopbtn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    stopbtn2.adjustsImageWhenHighlighted=YES;
    stopbtn2.tag=3;
   [self.window addSubview:_uiimageview1];
    [self.window  addSubview:imageview];
    [self.window addSubview:stopbtn];
    [self.window addSubview:stopbtn1];
    [self.window addSubview:stopbtn2];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

-(void)btnClick:(id)sender
{
    UIButton *btn=(UIButton *)sender;
    if (btn.tag==1) {
        [_uiimageview1 stopAnimating];
    }else if(btn.tag==3)
    {
        [_uiimageview1 startAnimating];
    }else if(btn.tag==2){
   
    if ([_uiimageview1 isAnimating]) {
        NSLog(@"正在动画中");
    }else{
        NSLog(@"不在动画中");
    }
    }

}


### Spring Security入门案例与黑马程序员教程 Spring Security 是一个强大的安全框架,用于保护基于 Java 的应用程序。它提供了认证、授权和防止常见攻击的功能。以下是一个简单的入门案例,结合了 Spring Boot 和 Spring Security 的基本配置。 #### 1. 引入依赖 在 `pom.xml` 文件中添加 Spring Security 的启动器依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 引入此依赖后,Spring Security 将自动为项目提供基础的安全保护[^2]。 #### 2. 配置安全规则 创建一个配置类来定义访问规则。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 允许所有用户访问 /public 路径 .anyRequest().authenticated() // 其他路径需要认证 .and() .formLogin() .loginPage("/login") // 自定义登录页面 .permitAll() .and() .logout() .permitAll(); } } ``` 上述代码定义了访问规则:`/public/**` 路径对所有用户开放,而其他路径需要经过身份验证才能访问[^2]。 #### 3. 创建用户认证信息 可以通过内存中的用户数据进行简单认证。例如: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class AuthConfig { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password(passwordEncoder().encode("password")).roles("USER") .and() .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN"); } } ``` 这里定义了两个用户:`user` 和 `admin`,分别拥有不同的角色权限[^2]。 #### 4. 创建控制器 为了测试安全规则,可以创建一个简单的控制器: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/public/hello") public String publicHello() { return "Hello, this is a public resource!"; } @GetMapping("/private/hello") public String privateHello() { return "Hello, this is a private resource!"; } } ``` 访问 `/public/hello` 不需要认证,而访问 `/private/hello` 则需要认证。 #### 5. 测试应用 运行 Spring Boot 应用程序后,尝试访问以下 URL: - `/public/hello`:可以直接访问。 - `/private/hello`:将被重定向到登录页面,输入用户名和密码后可以访问。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值